Learning Swift 3.0 - 从精通到重新入门
前言Swift 3.0是 Swift 加入到开源社区以来,第一次发布大版本。 作为下一代的Apple官方语言,从 Swift 2 开始,已经算是一门比较完善的语言了,完全可以当做iOS和macOS开发的主力,Swift 1 > 2 > 3 的改变,可以看出Apple的思路:
let path = CGMutablePath()
path.move(transform: &transform,x: topLeft.x,y: topLeft.y)
总的感觉就是:出道这么久,Swift要自立门户了。 API命名简化
// old code (Swift 2.2)
let content = text.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
// new code (Swift 3.0)
let content2 = text.trimmingCharacters(in: .newlines)
C式API替换很多Core打头和常用的C的api,在新的swift中全部重写成了swift风格的,如下: GCD api// old way,Swift 2
let queue = dispatch_queue_create("com.test.myqueue",nil)
dispatch_async(queue) {
print("Hello World")
}
// new way,Swift 3
let queue = DispatchQueue(label: "com.test.myqueue")
queue.asynchronously {
print("Hello World")
}
Core Graphics api// old way,Swift 2
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()
// new way,Swift 3
if let ctx = UIGraphicsGetCurrentContext() {
let rectangle = CGRect(x: 0,height: 512)
ctx.setFillColor(UIColor.blue().cgColor)
ctx.setStrokeColor(UIColor.white().cgColor)
ctx.setLineWidth(10)
ctx.addRect(rectangle)
ctx.drawPath(using: .fillStroke)
UIGraphicsEndImageContext()
}
大写开头的enum变成小写// old way,Swift 2,followed by new way,Swift 3
UIInterfaceOrientationMask.Landscape
UIInterfaceOrientationMask.landscape
NSTextAlignment.Right
NSTextAlignment.right
SKBlendMode.Multiply
SKBlendMode.multiply
取消NS前缀NSURL变为URL,NSDate变为Date… 参数label表现一致在Swift2.2里:函数定义里的第一个参数如果不显式指定label,调用时默认是没有label的: func someFunc(arg0: String,arg1: String)
func anotherFunc(_ arg0: String,arg1: String)
//调用时
someFunc("first",arg1: "second")
anotherFunc("first",arg1: "second")
如上例,两种声明方式效果相同。 而在Swift3中,label的生成方式变得一致,上面的例子会变成: func someFunc(arg0: String,arg1: String)
//调用时
someFunc(arg0: "first",arg1: "second")
隐式解包可选(ImplicitlyUnwrappedOptional)SE-0054 的提议寻求减少隐式解包可选类型(ImplicitlyUnwrappedOptional,IUO,类似于Int!的声明方式)的使用,因为Swift的类型设计本能十分安全,IUO破坏了这种安全性,现在,只可以在以下几个地方使用隐式可选类型:
在使用中IUO的行为也有改变↓ func f() -> Int! { return 3 } // f: () -> Int?,has IUO attribute
let x1 = f() // succeeds; x1: Int? = 3
let x2: Int? = f() // succeeds; x2: Int? = .some(3)
let x3: Int! = f() // succeeds; x3: Int? = .some(3),has IUO attribute
let x4: Int = f() // succeeds; x4: Int = 3
let a1 = [f()] // succeeds; a: [Int?] = [.some(3)]
let a2: [Int!] = [f()] // illegal,nested IUO type
let a3: [Int] = [f()] // succeeds; a: [Int] = [3]
func g() -> Int! { return nil } // f: () -> Int?,has IUO attribute
let y1 = g() // succeeds; y1: Int? = .none
let y2: Int? = g() // succeeds; y2: Int? = .none
let y3: Int! = g() // succeeds; y3: Int? = .none,has IUO attribute
let y4: Int = g() // traps
let b1 = [g()] // succeeds; b: [Int?] = [.none]
let b2: [Int!] = [g()] // illegal,nested IUO type
let b3: [Int] = [g()] // traps
func p<T>(x: T) { print(x) }
p(f()) // prints "Optional(3)"; p is instantiated with T = Int?
if let x5 = f() {
// executes,with x5: Int = 3
}
if let y5 = g() {
// does not execute
}
类方法当你使用类方法或者类属性时,之前都必须像这样做: CustomStruct.staticMethod() struct CustomStruct {
static func staticMethod() { ... }
func instanceMethod()
Self.staticMethod() // in the body of the type
}
}
let customStruct = CustomStruct()
customStruct.Self.staticMethod() // on an instance of the type
去掉了C风格的For循环for(var i = 0; i < 10; i++)
这个不提的话我想大部分用Swift的人都不会注意到吧。 去掉了柯里化语法因为这个语法不容易理解,所以去掉了。 去掉了
|