准备好迎接 3.0 API 变化
据 Chris Lattner 所说,即将发布的 Swift 3 将在我们熟悉的 Cocoa 和 CocoaTouch 上做出重大改变。
这一变化包含在将 Objective-C API 转化成 Swift 的提案 SE-0005 中。由于属性名结尾的单词和属性类型已经包含了足够的信息,因此可以删掉 class func darkGrayColor() -> UIColor // 因此 foregroundColor = .darkGrayColor() // 变成了 foregroundColor = .darkGray() 我简单总结了几条简化规则。记住,如果以下规则(进行简化)产生了一个不合法的结果(空的 简化 #1:去除相同类型成员前边的类型名称: let c = myColor.colorWithAlphaComponent(0.5) // 变为 let c = myColor.withAlphaComponent(0.5) 简化 #2:如果类型名后是 by + let img = myImage.imageByApplyingOrientation(o) // 变为 let img = myImage.applyingOrientation(o) 简化 #3:当方法选择器中含有类型名并满足以下情况时,去掉类型名称: | 尾部位于: | 需精简部分: | documentForURL(_ url: NSURL) var parentContext: NSManagedObjectContext? class func darkGrayColor() -> UIColor // 变为 documentFor(_ url: NSURL) var parent: NSManagedObjectContext? class func darkGray() -> UIColor 简化 #4:去掉方法名中动词后的类型名: myVC.dismissViewControllerAnimated(...) // 变为 myVC.dismissAnimated(...)
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |