經歷了從swift 1.0 到2.0,一個版本以後代碼竟然就不兼容了。這如何在團隊推廣呢?沒有想到3.0竟然變化更加的大。有多大,來體會一下:python
UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline) UIFont.preferredFont(forTextStyle: UIFontTextStyleSubheadline) override func numberOfSectionsInTableView(tableView: UITableView) -> Int override func numberOfSections(in tableView: UITableView) -> Int
在swift 2.x的時代基本上ObjC的接口是什麼樣的,那麼swift的方法名稱也是同樣的。npm
在swift發佈的時候,其實不少人都發現其語法有不少腳本語言的特徵。可是方法名稱仍是保留着ObjC的「見名知義」的特徵,那叫一個長,把這個方法的功能裏裏外外都說明的很是清楚。可是,其實這些沒有徹底的必要。因此在swift 3.0裏使用方法裏參數的lable來完成說明方法功能的做用。swift
所謂「去掉多餘文字」就是把原來iOS SDK方法名稱裏的描述性文字都移到方法的label裏面。而且原來方法第一個參數的label能夠不寫的,如今全部label在調用的時候都須要給出,除非特殊說明。這樣的修改就大大的縮短了方法名。app
attributedString.appendAttributedString(anotherString) attributedString.append(anotherString) names.insert("Jane", atIndex: 0) names.insert("Jane", at: 0) UIDevice.currentDevice() UIDevice.current()
如上所述,方法的第一個參數的label在swift2.x版本里調用的時候是不用寫的,可是在3.0版本必須給出。async
NSTimer.scheduledTimerWithTimeInterval(0.35, target: self, selector: #selector(reset), userInfo: nil, repeats: true) NSTimer.scheduledTimer(timeInterval: 0.35, target: self, selector: #selector(reset), userInfo: nil, repeats: true)
若是說你在本身定義的方法在調用的時候不須要label,那麼須要顯式的用下劃線「_」代表。ide
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { ... } override func didMoveToView(_ view: SKView) { ... }
這是千呼萬喚始出來的修改。以前對於c接口的調用基本上保持了和ObjC調用一致的風格:工具
let ctx = UIGraphicsGetCurrentContext() let rectangle = CGRect(x: 0, y: 0, width: 512, height: 512) CGContextSetFillColorWithColor(ctx, UIColor.blueColor().CGColor) CGContextSetStrokeColorWithColor(ctx, UIColor.whiteColor().CGColor) CGContextSetLineWidth(ctx, 10) CGContextAddRect(ctx, rectangle) CGContextDrawPath(ctx, .FillStroke) UIGraphicsEndImageContext()
在swift3.0中也改形成了swift風格的API:性能
if let ctx = UIGraphicsGetCurrentContext() { let rectangle = CGRect(x: 0, y: 0, width: 512, height: 512) ctx.setFillColor(UIColor.blue().cgColor) ctx.setStrokeColor(UIColor.white().cgColor) ctx.setLineWidth(10) ctx.addRect(rectangle) ctx.drawPath(using: .fillStroke) UIGraphicsEndImageContext() }
還有GCD部分的API也已經改造。GCD是徹底用C寫的一個叫作libdispatch的庫。在swfit3.0中是這樣的:spa
let queue = DispatchQueue(label: "com.test.myqueue") queue.async { print("Hello World") }
與以前的調用方式差異很大,以前是這樣的:code
let queue = dispatch_queue_create("com.test.myqueue", nil) dispatch_async(queue) { print("Hello World") }
在一個方法能夠接受另一個方法做爲參數傳入的時候,這個方法的定義在swift2.0裏是這樣的:
func g(a: Int -> Int) -> Int -> Int { ... }
a: Int -> Int
a是一個接受一個Int參數,返回一個Int值的方法的定義。在swift3.0裏是這樣定義的:
func g(a: (Int) -> Int) -> (Int) -> Int { ... }
更加易讀。至少能看出來接受一個Int型參數了。
以上是一些常常會接觸到的改變。其餘的改變還有性能的提高,和編譯後APP說起的縮減。這些不是一眼能看見的改變也是很是的巨大的。可是,更加有魅力也更加實用的改變是Swift Package Manager
有了這個工具就能夠直接像js的npm,python的pip同樣,一個命令搞定所有包和包的依賴項。頓時感受天空一片晴朗有木有!
另外還有很重要的一點。swift已經發展到必定的程度,語言自己已經基本定型。因此從這個版本開始swift社區把代碼的兼容放在一個比較靠前的位置來考慮了。至少按照官方的說法是不到萬不得已不破壞代碼的向前兼容(最前也就到swift3.0了)。能夠考慮在在團隊中引入swift了。