這里是一些從2.2到3.0的筆記。
[Swift 3.0 Migration Guide] https://swift.org/migration-guide/
指針
1.UnsafePointer, UnsafeMutablePointer, AutoreleasingUnsafeMutablePointer, OpaquePointer, Selector, NSZone等指針屬性不能為nil,如需為nil,必須是Optional;
2.memory屬性變為pointee;
3.用Int(bitPattern: nullablePointer)來表示傳給C語言的指針類型;
4.引入 Unsafe[Mutable]RawPointer類型,用于安全地進行指針的類型轉換
5.COpaquePointer 現在重命名為 OpaquePointer集合類型
1.Range<>.reversed 失效,如需實現相似功能,使用<Collection>[<Range>].indices.reversed()方法;
- Index有很多方法都被挪去 Collection 類型里的方法,如 Collection.index(index, offsetBy: delta), Collection.index(index, offsetBy: delta, limitedBy: otherIndex), Collection.distance(from: index, to: otherIndex)等;
- Swift Foundation
1.使用struct 來構建Notification:static let MyNotification = Notification.Name("MyNotification");
2.由于這些原來用 String 來表達的類型現被封裝成 struct,在一些地方需要用.rawValue 來傳參;
3.如果需要使用 URL 相關的API,需要手動改寫,Swift Migrator 不會幫你轉換;
4.原來的 (x as NSDate).earlierDate(y) 現在寫成 x < y ? x : y;原來的 (x as NSDate).laterDate(y) 現在寫成 x < y ? y : x;
- dispatch_once棄用,現在 lazily initialized 的閉包可以保證一次運行和線程安全;
6.Closure 現在默認是non-escaping ,因此可能需要用到@escaping
7.OC 的 id 類型現在是Any 類型