- ++ 和 --
以前:
var i = 0
i++
++i
i--
--i
現(xiàn)在:
var i = 0
i += 1 // 或 i = i + 1
i -= 1 // 或 i = i - 1
- C語(yǔ)言風(fēng)格的for循環(huán)
以前:
for (i = 1; i <= 10; i++) {
print(i)
}
現(xiàn)在:
for i in 1...10 {
print(i)
}
或者
(1...10).forEach {
print($0)
}
- 移除函數(shù)參數(shù)的
var
旨椒,避免和inout
混淆
以前:
func gcd(var a: Int, var b: Int) -> Int {
if (a == b) {
return a
}
repeat {
if (a > b) {
a = a - b
} else {
b = b - a
}
} while (a != b)
return a
}
現(xiàn)在:
func gcd(a: Int, b: Int) -> Int {
if (a == b) {
return a
}
var c = a
var d = b
repeat {
if (c > d) {
c = c - d
} else {
d = d - c
}
} while (c != d)
return c
}
- 默認(rèn)添加調(diào)用函數(shù)時(shí)的第一個(gè)參數(shù)標(biāo)簽
以前:
gcd(8, b: 12)
現(xiàn)在:
gcd(a: 8, b: 12)
現(xiàn)在遷移到以前:第一個(gè)參數(shù)標(biāo)簽為_(kāi)
func gcd(_ a: Int, b: Int) -> Int {
}
- 移除用
String
生成Selector
条篷,用#selector
替代
以前:
button.addTarget(responder, action: "tap", forControlEvents: .TouchUpInside)
現(xiàn)在:
button.addTarget(responder, action: #selector(Responder.tap), for: .touchUpInside)
- 移除用String生成keyPath
以前:
let me = Person(name: "Cosmin")
me.valueForKeyPath("name")
現(xiàn)在:
let me = Person(name: "Cosmin")
me.value(forKeyPath: #keyPath(Person.name))
- Foundation 類(lèi)型去除NS前綴
以前:
let file = NSBundle.mainBundle().pathForResource("tutorials", ofType: "json")
let url = NSURL(fileURLWithPath: file!)
let data = NSData(contentsOfURL: url)
let json = try! NSJSONSerialization.JSONObjectWithData(data!, options: [])
print(son)
現(xiàn)在:
let file = Bundle.main().pathForResource("tutorials", ofType: "json")
let url = URL(fileURLWithPath: file!)
let data = try! Data(contentsOf: url)
let json = try! JSONSerialization.jsonObject(with: data)
print(son)
- M_PI vs .pi
以前:
let r = 3.0
let circumference = 2 * M_PI * r
let area = M_PI * r * r
現(xiàn)在:
Float.pi
Double.pi
CGFloat.pi
let r = 3.0
let circumference = 2 * Double.pi * r
let area = Double.pi * r * r
或使用類(lèi)型推斷
let r = 3.0
let circumference = 2 * .pi * r
let area = .pi * r * r
- GCD
以前:
let queue = dispatch_queue_create("Swift 2.2", nil)
dispatch_async(queue) {
print("Swift 2.2 queue")
}
現(xiàn)在:
let queue = DispatchQueue(label: "Swift 3")
queue.async {
print("Swift 3 queue")
}
- Core Graphics
以前:
override func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
let blue = UIColor.blueColor().CGColor
CGContextSetFillColorWithColor(context, blue)
let red = UIColor.redColor().CGColor
CGContextSetStrokeColorWithColor(context, red)
CGContextSetLineWidth(context, 10)
CGContextAddRect(context, frame)
CGContextDrawPath(context, .FillStroke)
}
現(xiàn)在:
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else {
return
}
let blue = UIColor.blue().cgColor
context.setFillColor(blue)
let red = UIColor.red().cgColor
context.setStrokeColor(red)
context.setLineWidth(10)
context.addRect(frame)
context.drawPath(using: .fillStroke)
}
- 動(dòng)詞 和 名詞
- 方法返回值 - 名詞
for i in (1...10).reversed() {
print(i)
}
var array = [1, 5, 3, 2, 4]
for (index, value) in array.enumerated() {
print("\(index + 1) \(value)")
}
var array = [1, 5, 3, 2, 4]
let sortedArray = array.sorted()
print(sortedArray)
- 方法改變值 - 動(dòng)詞
var array = [1, 5, 3, 2, 4]
array.sort()
print(array)
- Swift API
省略不必要的文字懦鼠,通過(guò)上下文推斷导梆。
- XCPlaygroundPage.currentPage becomes PlaygroundPage.current
- button.setTitle(forState) becomes button.setTitle(for)
- button.addTarget(action, forControlEvents) becomes button.addTarget(action, for)
- NSBundle.mainBundle() becomes Bundle.main()
- NSData(contentsOfURL) becomes URL(contentsOf)
- NSJSONSerialization.JSONObjectWithData() becomes JSONSerialization.jsonObject(with)
- UIColor.blueColor() becomes UIColor.blue()
- UIColor.redColor() becomes UIColor.red()
- 枚舉
小寫(xiě)的駝峰標(biāo)識(shí)替代大小的駝峰標(biāo)識(shí)
- .System becomes .system
- .TouchUpInside becomes .touchUpInside
- .FillStroke becomes .fillStroke
- .CGColor becomes .cgColor
訪問(wèn)級(jí)別
公開(kāi)(public)
內(nèi)部(internal)
文件外私有(fileprivate)
私有(private):即使是在同一個(gè)文件當(dāng)中搀别,私有成員也只能夠在對(duì)應(yīng)的作用域當(dāng)中訪問(wèn)退个。將 inout, @noescape 和 @autoclosure 聲明調(diào)整為類(lèi)型修飾
func double(input: inout Int) {
input = input * 2
}
func noEscape(f: @noescape () -> ()) {}
func noEscape(f: @autoclosure () -> ()) {}
- 移除柯里化函數(shù)聲明語(yǔ)法
以前:
func curried(x: Int)(y: Int) -> Int {
return {(y: Int) -> Int in
return x * y
}
}
現(xiàn)在:
func curried(x: Int) -> (y: Int) -> Int {
return {(y: Int) -> Int in
return x * y
}
}
- 允許(絕大多數(shù))關(guān)鍵詞作為參數(shù)標(biāo)簽
// Swift 3 calling with argument label:
calculateRevenue(for sales: numberOfCopies,
in .dollars)
// Swift 3 declaring with argument label:
calculateRevenue(for sales: Int,
in currency: Currency)