引言
Apple與今年6月13日正式發(fā)布了Swift3.0的第一個預(yù)覽版本胰耗,并且相應(yīng)推出了Xcode8的第一個bate版本。開發(fā)者已經(jīng)可以在Xcode8bate版上來體驗Swift3.0的新特性吏廉。首先,Swift3.0確實帶來了很大改變,許多Swift中的結(jié)構(gòu)體API都進行了更新,例如String雹嗦,Array等,Swift3.0版本將許多類Objective-C風(fēng)格的API都更換成了Swift風(fēng)格的合是,其目的使開發(fā)者可以使用Swift更加愜意有趣的編程了罪。本系列博客,是我觀看WWDC視頻中介紹的內(nèi)容以及Swift3.0的開發(fā)者幫助文檔整理總結(jié)而來聪全,在期間泊藕,我也參考對比了Swift2.2中的實現(xiàn)方式,希望可以幫助需要的朋友盡快熟悉和上手Swift3.0难礼。
API變化
去掉
i++ ++i i-- --i
語法,開發(fā)者可以用i+=1 i-=1
代替娃圆。for循環(huán):
*** 摒棄C風(fēng)格的for循環(huán):***
for (var i = 0; i < 10; i++){print)(i)}
*** 常用的for循環(huán)寫法:***
//[1-10]
for (i in 1...10) {print(i)}
//[1,10)
for (i in 1...<10) {print(i)}
//[10, 1]
for (i in 1...10).reversed() {print(i)}
//(10, 1]
for (i in 1...<10).reversed() {print(i)}
*** 用stride
關(guān)鍵字描述for循環(huán)(by的意思是步長): ***
// 從0到10 步長為2玫锋,to是開區(qū)間,不包括10踊餐,所以控制臺打印 0 2 4 6 8
for i in stride(from:0, to:10, by:2){print(i)}
// 從0到10 步長為2景醇,through是閉區(qū)間,包括10吝岭,所以控制臺打印 0 2 4 6 8 10
for i in stride(from:0, through:10, by:2){print(i)}
// 從10到0步長為0.1
for i in stride(from:10, through:0, by:0.1){print(i)}
// 從10.5到0步長為0.1
for i in stride(from:10.5, through:0, by:0.1){print(i)}
- 元組
*** 元組(tuples)把多個值組合成一個復(fù)合值三痰。元組內(nèi)的值可以使任意類型,并不要求是相同類型 ***
// httpError 的類型是 (Int, String),值是 (404, "Not Found")
let httpError = (404, "Not Found")
// 把元組的內(nèi)容分解成單獨的常量和變量
let (name, age) = ("wang", 3)
print("The name is \(name)") // 打印wang
// 如果你只需要一部分元組值,分解的時候可以把要忽略的部分用下劃線(_)標記:
let (name, _) = ("wang", 3)
print("The name is \(name)") // 打印wang
// 定義元組給每個元素單獨起名字:
let person = (name:"wang", age:3)
print("The name is \(person.name)") // 打印wang
*** 元組取值方式 ***
let person = ("wang", 3)
print("The name is \(person.1)") // 打印3
let person = (name:"wang", age:3)
print("The name is \(person.name)") // 打印wang
*** 元組的比較***
// 通過下面兩組比較不難發(fā)現(xiàn),元組是通過維度來比較值的窜管。例如:如果Math的值不相等散劫,則score0與score1的比較結(jié)果是依據(jù)Math的值決定的;如果Math的值相等幕帆,則score0與score1的比較結(jié)果是依據(jù)English的值決定的获搏。多個維度的也是按照此方式進行比較的,大家可以嘗試一下失乾。
//1.第1組:
let score0 = (Math:100, English:100)
let score1 = (Math:99, English:100)
print(score0 == score1) // 打印false
print(score0 > score1) // 打印true
//2.第2組:
let score0 = (Math:100, English:99)
let score1 = (Math:100, English:100)
print(score0 == score1) // 打印false
print(score0 > score1) // 打印false
print(score0 < score1) // 打印true
// 元組運算符的重載機制常熙。那什么時候需要這個重載呢?例如:我想拿出元組中English的值來衡量score0和score1的大小碱茁,而不是按照從左到右的維度來比較裸卫。
let score0 = (Math:1, English:100)
let score1 = (Math:89, English:100)
// 在Swift中運算符本質(zhì)上是函數(shù)
func <(s1:(Int, Int), s2:(Int, Int)) ->Bool {
if s1.1 != s2.1 {
return s1.0 < s2.0
}
return s1.1 < s2.1
}
print(score0 < score1) // 打印false
// 元組函數(shù)傳值
func printScore(Math:Int, English:Int) {
print("The Math Score is\(Math), The English Score is\(English)")
}
//在Swift2.0中是這樣調(diào)用的:
printScore(score0)
// 在Swift3.0中是這樣調(diào)用的:
printScore(score0.Math, score0.English)
- Selector語法
想必有過OC開發(fā)經(jīng)驗的同學(xué)應(yīng)該都會知道selector的含義是啥吧,這里不再贅述
Swift3.0可以在playground中直接支持顯示和操作界面
使用之前需要
import PlaygroudSupport
纽竣,不然沒法使用墓贿。
點擊View-Assistant Editor-show Assisitant Editor調(diào)出UI界面
Snip20170131_3.png
Snip20170131_2.png
貼代碼:
class ViewController:UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
let btn = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
btn.setTitle("haha", for: UIControlState.normal);
btn.backgroundColor = UIColor.blue
//這個#selector()是Swift3.0新的寫法,寫錯或者不實現(xiàn)方法都會報錯提示蜓氨,比起Swift2輸入字符串的方式更加智能聋袋。
btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
self.view.addSubview(btn)
}
func click(button:UIButton) {
if button.backgroundColor == UIColor.blue {
button.backgroundColor = UIColor.red
} else {
button.backgroundColor = UIColor.blue
}
}
}
var vc = ViewController()
vc.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
PlaygroundPage.current.liveView = vc.view