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下设置