Swift 3 新特性

函數和方法的第一個參數老是有標籤,除非使用「_」來省掉

func method(fromX x: Int, toY y: Int) {}
method(fromX: 0, toY: 0)
func method2(_ x: Int, y: Int) {}
method2(0, y: 0)
func method3(x: Int, y: Int) {}
method3(x: 0, y: 0)
// old: typealias CompleteHandler = (token: String, error: Error?) -> Void
typealias CompleteHandler = (_ token: String, _ error: String?) -> Void

OC API中的id如今都轉到了Any,而再也不是AnyObject。

Swift的Any類型能夠處理任何類型(包括枚舉,結構體,元組,類),AnyHashable能夠做爲Set,Dictionary的鍵
NSArray, NSDictionary, NSSet分別對應[Any] [AnyHashable:Any] Set[AnyHashabel]
NSCopying, NSMutableCopying協議的copy(with:),mutableCopy(with:)都返回Anyhtml

fileprivate & private

新增長fileprivate, 若是用來標記類的方法和屬性,private變成只能在class類定義內使用,不能在extension中使用;而fileprivate即之前的private,能夠在本文件內的extension中使用。swift

許多早期庫函數被省掉了多餘部分

UIColor.black   // old: blackColor
var array = ["hello", "world"]
array.insert("haha", at: 2) // old: atIndex

枚舉值使用「小駱駝拼寫法」

NSTextAlignment.rightapi

動詞和名詞的使用更加一致(加ed,ing後綴認爲是名詞)

array.enumerated() // n.返回一個枚舉的拷貝,old: enumerate()
array.sort() // v.將本身排序
array.sorted() // n.返回一個排序好的拷貝閉包

函數的定義和使用都要加上括號

func g(a: Int) -> Int { return 1 }
func g3(a: (Int) -> Int) -> (Int) -> Int { return g }
// old: func g2(a: Int -> Int) -> Int -> Int { return g }

基礎類中去除了OC風格的前綴NS

let date = Date() // NSDate()

函數形參取消了var,inout修飾置於":"後

// func foo(var i: Int)會報錯
func foo(i: Int) {} // i是let的,不能被改變
func foo2(i: inout Int) {}

協議的可選方法也須要加上@objc

@objc protocol MyProtocol {
    @objc optional func func1() // old: optional func func1()
}

取消一些C風格

  • 移除++,--操做符 i++; i--;app

  • 移除C風格for循環 for var i = 0; i < 10; i += 1 {}async

  • 移除XXMake()這種建立方式 如,CGRectMake函數

let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
  • GCD,Core Graphics取消C風格性能

let queue = DispatchQueue(label: "com.test.myqueue")
queue.async {
    print("haha")
}
  • 一些常量定義移到枚舉內部優化

UserDefaults.didChangeNotification // old: NSUserDefaultsDidChangeNotification

閉包escaping

swift3中,函數參數的默認閉包是非逃逸的,不須要加@noescape,若是是逃逸閉包須要添加@escaping。ui

啓用全模塊編譯優化(WMO)

能夠提高編譯性能,減小編譯時間,在Release模式下開啓,Debug下不推薦。
Build Settings/Swift Compiler - Optimization Level下設置

引用:

Swift3.0 Release

XCode8.1 Release Note

全模塊編譯優化

相關文章
相關標籤/搜索