經(jīng)歷了從swift 1.0 到2.0,一個版本之后代碼居然就不兼容了。這如何在團隊推廣呢?沒有想到3.0居然變化更加的大。有多大,來體會一下:
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的方法名稱也是一樣的。
在swift發(fā)布的時候,其實很多人都發(fā)現(xiàn)其語法有很多腳本語言的特征。但是方法名稱還是保留著ObjC的“見名知義”的特征,那叫一個長,把這個方法的功能里里外外都說明的非常清楚。但是,其實這些沒有完全的必要。所以在swift 3.0里使用方法里參數(shù)的lable來完成說明方法功能的作用。
去掉多余文字
所謂“去掉多余文字”就是把原來iOS SDK方法名稱里的描述性文字都移到方法的label里面。并且原來方法第一個參數(shù)的label可以不寫的,現(xiàn)在所有l(wèi)abel在調用的時候都需要給出,除非特殊說明。這樣的修改就大大的說短了方法名。
attributedString.appendAttributedString(anotherString)
attributedString.append(anotherString)
names.insert("Jane", atIndex: 0)
names.insert("Jane", at: 0)
UIDevice.currentDevice()
UIDevice.current()
第一個參數(shù)的label
如上所述,方法的第一個參數(shù)的label在swift2.x版本里調用的時候是不用寫的,但是在3.0版本必須給出。
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,那么需要顯式的用下劃線“_”表明。
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { ... }
override func didMoveToView(_ view: SKView) { ... }
對SDK里C的改造
這是千呼萬喚始出來的修改。之前對于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也已經(jīng)改造。GCD是完全用C寫的一個叫做libdispatch的庫。在swfit3.0中是這樣的:
let queue = DispatchQueue(label: "com.test.myqueue")
queue.async {
print("Hello World")
}
與之前的調用方式差別很大,之前是這樣的:
let queue = dispatch_queue_create("com.test.myqueue", nil)
dispatch_async(queue) {
print("Hello World")
}
方法類型
在一個方法可以接受另外一個方法作為參數(shù)傳入的時候,這個方法的定義在swift2.0里是這樣的:
func g(a: Int -> Int) -> Int -> Int { ... }
a: Int -> Inta是一個接受一個Int參數(shù),返回一個Int值的方法的定義。在swift3.0里是這樣定義的:
func g(a: (Int) -> Int) -> (Int) -> Int { ... }
更加易讀。至少能看出來接受一個Int型參數(shù)了。
最后
以上是一些經(jīng)常會接觸到的改變。其他的改變還有性能的提升,和編譯后APP提及的縮減。這些不是一眼能看見的改變也是非常的巨大的。但是,更加有魅力也更加實用的改變是Swift Package Manager有了這個工具就可以直接像js的npm,python的pip一樣,一個命令搞定全部包和包的依賴項。頓時感覺天空一片晴朗有木有!
另外還有很重要的一點。swift已經(jīng)發(fā)展到一定的程度,語言本身已經(jīng)基本定型。所以從這個版本開始swift社區(qū)把代碼的兼容放在一個比較靠前的位置來考慮了。至少按照官方的說法是不到萬不得已不破壞代碼的向前兼容(最前也就到swift3.0了)??梢钥紤]在在團隊中引入swift了。3.0