[关闭]
@wrlqwe 2016-06-19T15:55:35.000000Z 字数 4291 阅读 906

Learning Swift 3.0 - 从精通到重新入门

Swift


前言

Swift 3.0是 Swift 加入到开源社区以来,第一次发布大版本。

作为下一代的Apple官方语言,从 Swift 2 开始,已经算是一门比较完善的语言了,完全可以当做iOS和macOS开发的主力,Swift 1 > 2 > 3 的改变,可以看出Apple的思路:

  1. let path = CGMutablePath()
  2. path.move(transform: &transform, x: topLeft.x, y: topLeft.y)

总的感觉就是:出道这么久,Swift要自立门户了。
广大开发者们常说:Swift 3.0 - 从精通到重新入门,这一天终于来了。
现在来看看Swift 3 有什么新变化吧,以下只是自己总结,不太全,未完待续↓

API命名简化

  1. // old code (Swift 2.2)
  2. let content = text.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
  3. // new code (Swift 3.0)
  4. let content2 = text.trimmingCharacters(in: .newlines)

C式API替换

很多Core打头和常用的C的api,在新的swift中全部重写成了swift风格的,如下:

GCD api

  1. // old way, Swift 2
  2. let queue = dispatch_queue_create("com.test.myqueue", nil)
  3. dispatch_async(queue) {
  4. print("Hello World")
  5. }
  6. // new way, Swift 3
  7. let queue = DispatchQueue(label: "com.test.myqueue")
  8. queue.asynchronously {
  9. print("Hello World")
  10. }

Core Graphics api

  1. // old way, Swift 2
  2. let ctx = UIGraphicsGetCurrentContext()
  3. let rectangle = CGRect(x: 0, y: 0, width: 512, height: 512)
  4. CGContextSetFillColorWithColor(ctx, UIColor.blueColor().CGColor)
  5. CGContextSetStrokeColorWithColor(ctx, UIColor.whiteColor().CGColor)
  6. CGContextSetLineWidth(ctx, 10)
  7. CGContextAddRect(ctx, rectangle)
  8. CGContextDrawPath(ctx, .FillStroke)
  9. UIGraphicsEndImageContext()
  10. // new way, Swift 3
  11. if let ctx = UIGraphicsGetCurrentContext() {
  12. let rectangle = CGRect(x: 0, y: 0, width: 512, height: 512)
  13. ctx.setFillColor(UIColor.blue().cgColor)
  14. ctx.setStrokeColor(UIColor.white().cgColor)
  15. ctx.setLineWidth(10)
  16. ctx.addRect(rectangle)
  17. ctx.drawPath(using: .fillStroke)
  18. UIGraphicsEndImageContext()
  19. }

大写开头的enum变成小写

  1. // old way, Swift 2, followed by new way, Swift 3
  2. UIInterfaceOrientationMask.Landscape
  3. UIInterfaceOrientationMask.landscape
  4. NSTextAlignment.Right
  5. NSTextAlignment.right
  6. SKBlendMode.Multiply
  7. SKBlendMode.multiply

取消NS前缀

NSURL变为URL,NSDate变为Date...

参数label表现一致

在Swift2.2里:函数定义里的第一个参数如果不显式指定label,调用时默认是没有label的:

  1. func someFunc(arg0: String, arg1: String)
  2. func anotherFunc(_ arg0: String, arg1: String)
  3. //调用时
  4. someFunc("first", arg1: "second")
  5. anotherFunc("first", arg1: "second")

如上例,两种声明方式效果相同。

而在Swift3中,label的生成方式变得一致,上面的例子会变成:

  1. func someFunc(arg0: String, arg1: String)
  2. func anotherFunc(_ arg0: String, arg1: String)
  3. //调用时
  4. someFunc(arg0: "first", arg1: "second")
  5. anotherFunc("first", arg1: "second")

隐式解包可选(ImplicitlyUnwrappedOptional)

SE-0054 的提议寻求减少隐式解包可选类型(ImplicitlyUnwrappedOptional, IUO, 类似于Int!的声明方式)的使用,因为Swift的类型设计本能十分安全,IUO破坏了这种安全性,现在,只可以在以下几个地方使用隐式可选类型:

在使用中IUO的行为也有改变↓

  1. func f() -> Int! { return 3 } // f: () -> Int?, has IUO attribute
  2. let x1 = f() // succeeds; x1: Int? = 3
  3. let x2: Int? = f() // succeeds; x2: Int? = .some(3)
  4. let x3: Int! = f() // succeeds; x3: Int? = .some(3), has IUO attribute
  5. let x4: Int = f() // succeeds; x4: Int = 3
  6. let a1 = [f()] // succeeds; a: [Int?] = [.some(3)]
  7. let a2: [Int!] = [f()] // illegal, nested IUO type
  8. let a3: [Int] = [f()] // succeeds; a: [Int] = [3]
  9. func g() -> Int! { return nil } // f: () -> Int?, has IUO attribute
  10. let y1 = g() // succeeds; y1: Int? = .none
  11. let y2: Int? = g() // succeeds; y2: Int? = .none
  12. let y3: Int! = g() // succeeds; y3: Int? = .none, has IUO attribute
  13. let y4: Int = g() // traps
  14. let b1 = [g()] // succeeds; b: [Int?] = [.none]
  15. let b2: [Int!] = [g()] // illegal, nested IUO type
  16. let b3: [Int] = [g()] // traps
  17. func p<T>(x: T) { print(x) }
  18. p(f()) // prints "Optional(3)"; p is instantiated with T = Int?
  19. if let x5 = f() {
  20. // executes, with x5: Int = 3
  21. }
  22. if let y5 = g() {
  23. // does not execute
  24. }

类方法

当你使用类方法或者类属性时,之前都必须像这样做:

CustomStruct.staticMethod()
现在可以使用首字母大写的 Self来代替以前的写法,并且用类型的实例也能调用静态方法或者属性了:

  1. struct CustomStruct {
  2. static func staticMethod() { ... }
  3. func instanceMethod()
  4. Self.staticMethod() // in the body of the type
  5. }
  6. }
  7. let customStruct = CustomStruct()
  8. customStruct.Self.staticMethod() // on an instance of the type

去掉了C风格的For循环

  1. for(var i = 0; i < 10; i++)

这个不提的话我想大部分用Swift的人都不会注意到吧。

去掉了柯里化语法

因为这个语法不容易理解,所以去掉了。

去掉了++ --语法

Swift Package Manager

新的Swift包管理器类似于npm这种三方代码的包管理器,对Swift的平台化很有帮助,也许以后的iOS开发者不会再用Cocoapods管理Swift依赖,而是用spm,spm甚至能跨多个平台管理代码,结合Swift最近在各种平台上疯狂移植的新闻。能开发客户端,能写脚本,服务端的Swift库也已经有了好几个,感觉Swift就要变成一个全能的语言了。。

要上天了...

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注