Protocols

  • protocol定义了完成特定任务所需的method/properties/其他要求
  • protocol中没有实际的implementation
  • 可以被class, structure, enumeration使用
  • 满足protocol要求的type被称为“conform to that protocol”
  • 相当于Java的interface,但java的interface可以有default implementation
protocol Driveable{func turnLeft()func turnRight()func brake()//By default, methods are required, unless:@objc optional func reverse()
}
class Car : Driveable{}
//Can adopt more than 1 protocol:
class Bicycle : Driveable, TwoWheels{}
//If the class has a superclass:
class SomeClass: SomeSuperclass, FirstProtocol, AnotherProtocol {// class definition goes here
}

Delegation


一个类比:

  • Protocol: 职位要求
  • Delegator:老板
  • Delegate:实习生小王

老板 (delegator) 把ios开发的任务 (event) 交给实习生小王 (delegate) 完成,因为他知道小王的条件符合职位要求 (conforms to protocol)

Closures

  • 类似于c和objective-c中的blocks,其他编程语言中的lambdas
  • 函数是closures
//Closures: A block of functionality / code
func backward(_ s1:Stirng, _ s2: String) -> Bool{return s1 > s2
}let names = ["Brett", "Eddy", "Hilary", "Sophie", "Ray"]
names.sort(by: backward)//General Form
names.sort(by: {(s1:Stirng, s2: String) Bool inreturn s1 > s2
})//First Shortcut: delete type
names.sort(by: {(s1, s2) inreturn s1 > s2
})//Second Shortcut: remove return keyword
names.sort(by: {(s1, s2) in s1 > s2})//Third Shortcut: use implied variable name
names.sort(by: {$0 > $1})//Fourth Shortcut: if closure is the last argument, can remove argument label (trailing closures)
names.sort{$0 > $1}

[IOS Dev] 关于Protocols, Delegate, 和Closures相关推荐

  1. iOS Dev (60) 怎样实现 UITextView 中的 placeHolder

    iOS Dev (60) 怎样实现 UITextView 中的 placeHolder 作者:阿锐 地址:http://blog.csdn.net/prevention - 跟着你的 UITextVi ...

  2. iOS tableview的常用delegate和dataSource执行顺序

    在这次项目中遇到了一个特别奇葩的问题:表视图创建的cell在7以上的系统能正常运行显示,在模拟器上就不能正常实现......为解决这个问题,纠结了好久...... 对在7系统上不显示的猜测: 用mas ...

  3. iOS Dev (21) 用 AVPlayer 播放一个本地音频文件

    iOS Dev (21) 用 AVPlayer 播放一个本地音频文件 作者:CSDN 大锐哥 博客:http://blog.csdn.net/prevention 前言 这篇文章与上一篇极其相似,要注 ...

  4. iOS Dev (24) 最简单的M3U8播放器

    iOS Dev (24) 最简单的M3U8播放器 作者:CSDN 大锐哥 地址:http://blog.csdn.net/prevention 概述 用 MediaPlayer Framework 中 ...

  5. iOS视图控制器之间delegate传值

    2019独角兽企业重金招聘Python工程师标准>>> // ** *为了从ViewControllerB往回传值到ViewControllerA,我们需要使用协议(Protocol ...

  6. iOS视图控制器之间delegate传值教程

    之前在StackOverFlow上看到一篇讲传值(segue传值和delegate传值)的文章,感觉讲的非常清晰,就将delegate部分翻译了一下.有兴趣能够看看. 原文: http://stack ...

  7. iOS设计模式——委托(delegate)

    委托(delegate)也叫代理是iOS开发中常用的设计模式.我们借助于protocol(参考博文:objective-c协议(protocol))可以很方便的实现这种设计模式. 什么是代理? 苹果的 ...

  8. ios无法定位CLLocationManager Delegate方法不能被调用的问题

    最近使用ios定位功能,但是总是定位不成功,不能获取定位数据,找了好久原因,终于发现问题所在,原来是ios8之前的规则已经不适用了. 先从ios developer library 中找到相关的说明. ...

  9. 【IOS】委托(delegate)和协议(protocol)

    iOS的委托模式之前也接触过,总觉得自己理解的不够透彻,使用时磕磕绊绊,今天做一个有关协议和委托模式的总结 协议(protocol) 由于Objective_C的类不支持多继承,而类在实现时很多相似的 ...

最新文章

  1. 深入理解计算机系统:网络编程 上
  2. ICLR 2020:从去噪自编码器到生成模型
  3. java修饰符总结,java访问修饰符总结
  4. 大文件及文件夹上传(续)
  5. Linux C 中断言assert()使用简介
  6. PHP | 计算字符串中的单词总数
  7. 中间件——构筑复杂分布式应用的关键技术(转)
  8. 击溃音乐服务器第一人!周杰伦新歌首发,QQ音乐服务器一度崩溃
  9. 太难了,斯坦福AI报告曝光!全球190万会AI,中国有5万
  10. android nds模拟器窗口,安卓NDS模拟器drastic模拟器使用经验分享
  11. php phpexcel导入excel
  12. Python-Selenium自动化登陆QQ空间
  13. 苹果审核4.3如何解决?混淆?还是重新上架?用这招居然成功上架AppStore了!
  14. 用于Firefox的Google工具栏Beta 2发布
  15. Unity3d+C#斗地主学习探究1
  16. 通过关键词获取微博内容
  17. 在炼数成金报名的课程!加油!
  18. 201919102004张雪婷(第五次作业)
  19. Quartz定时任务-@DisallowConcurrentExecution注解
  20. 微信小程序之左右布局

热门文章

  1. oracle中累乘函数,oracle sql语句实现累加、累减、累乘、累除
  2. 计算机网络sp,网络信号电涌保护器(sp-005dc10-rj45) Network signal surge protector (sp-005dc10-rj45)...
  3. 简单3步,在线制作GIF宣传动图
  4. 大智慧5.60经典版
  5. luogu P3750 [六省联考 2017]分手是祝愿
  6. 文件权限修改方法(数字法)
  7. 关于shell脚本的知识点总结
  8. CSS前端基础知识梳理
  9. 永远不要去依赖别人_不要依赖别人的格言
  10. 小白学数据_|_28张小抄表大放送:Python,R,大数据,机器学习