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
Swift的Any類型能夠處理任何類型(包括枚舉,結構體,元組,類),AnyHashable能夠做爲Set,Dictionary的鍵
NSArray, NSDictionary, NSSet分別對應[Any] [AnyHashable:Any] Set[AnyHashabel]
NSCopying, NSMutableCopying協議的copy(with:),mutableCopy(with:)都返回Anyhtml
新增長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
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 }
let date = Date() // NSDate()
// func foo(var i: Int)會報錯 func foo(i: Int) {} // i是let的,不能被改變 func foo2(i: inout Int) {}
@objc protocol MyProtocol { @objc optional func func1() // old: optional func func1() }
移除++,--操做符 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
swift3中,函數參數的默認閉包是非逃逸的,不須要加@noescape,若是是逃逸閉包須要添加@escaping。ui
能夠提高編譯性能,減小編譯時間,在Release模式下開啓,Debug下不推薦。
Build Settings/Swift Compiler - Optimization Level下設置