swift3.0都改变了什么

经历了从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

如上所述,方法的第一个参数的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) { ... }

对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也已经改造。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 -> Inta是一个接受一个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了。

相关文章
相关标签/搜索