一块儿聊聊 Swift 3.0

Swift3.0将会给咱们带来哪些改变:

1. 稳定二进制接口(ABI)

ABI是什么呢?API你们都知道是应用程序接口 API只是提供函数签名
而ABI是系统和语言层面的 若是ABI稳定 意味着之后Swift版本更新升级 咱们不须要再修改老版本 Swift 语言编译的库了
若是你曾经从Swift 1.x 升级到 Swift 2.x 将会体会颇深php

2. 弹性/韧性 解决易碎二进制接口问题

Fragile binary interface problem是面向对象编程语言的通病 若是在程序中引入了外部库 咱们的的程序中使用并继承了该外部库中的类 若是外部库有改动 咱们必须从新编译全部该类的继承树 而这类问题被称为脆弱的基类 (Fragile base class)git

3. 可移植性

Swift可被移植到其余平台上 github

4. 全面支持泛型特性

Swift 2.2已经很好的支持泛型 可是还不够完善
Swift 3.0开始 将全面支持泛型的全部特性 编程

5. 聚焦和完善

尽管是一个相对年轻的语言,可是Swift的快速发展已经积累了必定的语言功能 Swift 3.0将会会删除或改善这些功能 从而提供更好的总体一致性swift

6. 新的API设计规范

Swift3.0 发布了新的语言设计规范 其中在Swift3.0中标准库和核心库将会遵循这个设计规范
设计规范地址:api

https://swift.org/documentation/api-design-guidelines/

关于设计规范我将会单独写一篇博客 感兴趣的记得关注个人公众号(DevTipss)或简书ruby

7. 即将删除 currying func(Swift柯里化(Currying)) 特性

在Swift3.0中 currying func 将会被移除 该提案在SE-0002被提出
提案给出的缘由是 currying func 用途是有限的而且增长了语言实现的复杂度app

8. 从函数参数中删除var关键字

缘由是var与inout会产生歧义和混乱编程语言

func doSomethingWithVar(var i: Int) { i = 2 // This will NOT have an effect on the caller's Int that was passed, but i can be modified locally } func doSomethingWithInout(inout i: Int) { i = 2 // This will have an effect on the caller's Int that was passed. } doSomethingWithVar(x) print(x) // 1 doSomethingWithInout(&x) print(x) // 2

doSomethingWithVar和doSomethingWithInout均可以在函数内部给i变量赋值 可是doSomethingWithVar并不能真正修改 i 的值
doSomethingWithInout能够真正修改 i 的值
var就产生了歧义和误导ide

9. 删除 ++-- 操做符
推荐使用+= 和 -=操做符
10. 为autoreleasepool添加错误处理

旧版autoreleasepool处理错误方式:

func doWork() throws -> Result { var result: Result? = nil var error: ErrorProtocol? = nil autoreleasepool { do { ... actual computation which hopefully assigns to result but might not ... } catch let e { error = e } } guard let result = result else { throw error! } return result! }

Swift3.0 autoreleasepool 处理错误方式

public func autoreleasepool<Result>(@noescape body: () throws -> Result) rethrows -> Result func doWork() throws -> Result { return try autoreleasepool { ... actual computation which either returns or throws ... } }
11. 容许直接引用(Default, Private, Repeat)关键字成员
enum UITableViewCellStyle : Int { case \`default\` case value1 case value2 case subtitle } enum SCNParticleImageSequenceAnimationMode : Int { case \ `repeat\` case clamp case autoReverse }

在Swift3.0以前咱们引用default和repeat成员时 须要这样写:

let cell = UITableViewCell(style: .`default`, reuseIdentifier: nil) particleSystem.imageSequenceAnimationMode = SCNParticleImageSequenceAnimationMode.`repeat`

Swift3.0时 容许咱们直接访问default repeat 关键字成员

let cell = UITableViewCell(style: .default, reuseIdentifier: nil) particleSystem.imageSequenceAnimationMode = SCNParticleImageSequenceAnimationMode.repeat
12. 将声明式@noescape和@autoclosure 改成类型属性
func f(@noescape fn : () -> ()) {} // declaration attribute //新的语法 func f(fn : @noescape () -> ()) {} // type attribute. func f2(a : @autoclosure () -> ()) {} // type attribute.
13. 重命名 Debug 标示符
__FILE__ -> #file __LINE__ -> #line __COLUMN__ -> #column __FUNCTION__ -> #function __DSO_HANDLE__ -> #dsohandle

Debug 标示符重命名后将会与#available #selector 关键字统一风格

参考:https://github.com/apple/swift-evolution



文/sprint(简书做者) 原文连接:http://www.jianshu.com/p/6a9e9e790064 著做权归做者全部,转载请联系做者得到受权,并标注“简书做者”。
相关文章
相关标签/搜索