一塊兒聊聊 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 著做權歸做者全部,轉載請聯繫做者得到受權,並標註「簡書做者」。
相關文章
相關標籤/搜索