随笔分类 -  swift

摘要:Swift 的 extension 机制很强大,不仅可以针对自定义的类型,还能作用于系统库的类型,甚至基础类型比如 Int。当在对系统库做 extension 的时候,就会涉及到一个命名冲突的问题。Objective-C 时代的通行解决办法是在扩展方法名字的最前面加上 XXX_ 形式的前缀。这种形式 阅读全文
posted @ 2018-09-25 17:42 zzfx 阅读(701) 评论(0) 推荐(0) 编辑
摘要:最近在看一些Swift开源库的时候,发现了一些优秀的开源库都使用了命名空间,例如Kingfisher这个开源库中,就针对UIImage,UIImageView,UIButton做了命名空间的扩展。通过logoImageView.kf.setImage(url)这种方式能够很好地避免扩展的命名冲突,而 阅读全文
posted @ 2018-09-25 17:40 zzfx 阅读(183) 评论(0) 推荐(0) 编辑
摘要:class GooClass { deinit { print("aaaaaaaa") } var str = "gooClass" } struct GooStruct { var goo = GooClass() } extension ViewController{ var gooStruct 阅读全文
posted @ 2018-09-25 15:06 zzfx 阅读(191) 评论(0) 推荐(0) 编辑
摘要:swift派发机制的核心是确定一个函数能否进入动态派发列表 阅读全文
posted @ 2018-09-21 14:50 zzfx 阅读(157) 评论(0) 推荐(0) 编辑
摘要:Swift Intermediate Language (SIL) https://github.com/apple/swift/blob/master/docs/SIL.rst#witness-method 阅读全文
posted @ 2018-09-20 17:59 zzfx 阅读(322) 评论(0) 推荐(0) 编辑
摘要:VTables https://github.com/apple/swift/blob/master/docs/SIL.rst#vtables decl ::= sil-vtable sil-vtable ::= 'sil_vtable' identifier '{' sil-vtable-entr 阅读全文
posted @ 2018-09-20 17:48 zzfx 阅读(308) 评论(0) 推荐(0) 编辑
摘要:In this example: protocol MyProtocol { func testFuncA() } extension MyProtocol { func testFuncA() { print("MyProtocol's testFuncA") } } class MyClass 阅读全文
posted @ 2018-09-20 14:49 zzfx 阅读(156) 评论(0) 推荐(0) 编辑
摘要:Swift protocol extension method is called instead of method implemented in subclass protocol MyProtocol { func methodA() func methodB() } extension My 阅读全文
posted @ 2018-09-20 14:48 zzfx 阅读(128) 评论(0) 推荐(0) 编辑
摘要:1、v-table; class 2、WitnessTable protocol 3、消息派发。 @objc dynamic 阅读全文
posted @ 2018-09-20 11:51 zzfx 阅读(288) 评论(0) 推荐(0) 编辑
摘要:顶级修饰 次级修饰 赋值类型 存储类型 值类型 值类型 深拷贝 栈 值类型 引用类型 浅拷贝 堆 引用类型 值类型 浅拷贝 堆 引用类型 引用类型 浅拷贝 堆 复合引用类型会改变内部值类型的存储行为。 以上内容为推测 阅读全文
posted @ 2018-09-20 11:41 zzfx 阅读(277) 评论(0) 推荐(0) 编辑
摘要:两种参数传递方式 值类型 传递的是参数的一个副本,这样在调用参数的过程中不会影响原始数据。 引用类型 把参数本身引用(内存地址)传递过去,在调用的过程会影响原始数据。 在 Swift 众多数据类型中,只有 class 是引用类型,其余的如 Int、Float、Bool、Character、Array 阅读全文
posted @ 2018-09-20 11:04 zzfx 阅读(441) 评论(0) 推荐(0) 编辑
摘要:1、检查protocol本体是否声明调用函数; 2、如果没有,检查protocol扩展是否有该函数;如果扩展中也没有,报错; 3、如果本体声明了函数,使用动态派发机制进行派发;扩展中的实现位于最末位。 阅读全文
posted @ 2018-09-19 19:30 zzfx 阅读(183) 评论(0) 推荐(0) 编辑
摘要:We learned in the Protocol-Oriented Programming session at WWDC 2015 that Swift uses two different dispatch mechanisms for methods in protocol extensi 阅读全文
posted @ 2018-09-19 19:27 zzfx 阅读(164) 评论(0) 推荐(0) 编辑
摘要:引用类型 (Reference Type Matters) 引用的类型决定了派发的方式. 这很显而易见, 但也是决定性的差异. 一个比较常见的疑惑, 发生在一个协议拓展和类型拓展同时实现了同一个函数的时候. protocol MyProtocol { } struct MyStruct: MyPro 阅读全文
posted @ 2018-09-19 19:11 zzfx 阅读(256) 评论(0) 推荐(0) 编辑
摘要:@objc vs @objc dynamic @objc: Objective-C entry points One can explicitly write @objc on any Swift declaration that can be expressed in Objective-C. @ 阅读全文
posted @ 2018-09-19 18:17 zzfx 阅读(368) 评论(0) 推荐(0) 编辑
摘要:以NSObject为基类,只是为了提供Objective-C API的使用入口; 经由@object修改的对象,是这些api的参量。 NSObject是swift与oc特有机制沟通的桥梁。 Subclassing NSObject in Swift gets you Objective-C runt 阅读全文
posted @ 2018-09-19 17:47 zzfx 阅读(1405) 评论(0) 推荐(0) 编辑
摘要:Objective-C entry points https://github.com/apple/swift-evolution/blob/master/proposals/0160-objc-inference.md Before Swift 4, the compiler made some  阅读全文
posted @ 2018-09-19 17:30 zzfx 阅读(307) 评论(0) 推荐(0) 编辑
摘要:1、扩展中无法继承重写已有函数,不能添加函数。 Extensions can add new functionality to a type, but they cannot override existing functionality. https://docs.swift.org/swift- 阅读全文
posted @ 2018-09-19 16:47 zzfx 阅读(876) 评论(0) 推荐(0) 编辑
摘要:class 是引用类型,生成的实例分布在 Heap(堆) 内存区域上,在 Stack(栈)只存放着一个指向堆中实例的指针。因为考虑到引用类型的动态性和 ARC 的原因,class 类型实例需要有一块单独区域存储类型信息和引用计数。 在 Swift 中,class 类型的方法派发是通过 V-Table 阅读全文
posted @ 2018-09-19 15:13 zzfx 阅读(238) 评论(0) 推荐(0) 编辑
摘要:New operators are declared at a global level using the operator keyword, and are marked with the prefix, infix or postfix modifiers: prefix operator + 阅读全文
posted @ 2018-09-17 11:34 zzfx 阅读(189) 评论(0) 推荐(0) 编辑