Swift--控制流与oc不同的地方
1.For-in循環(huán)中...
for index in 1...5 {
? ? print("\(index) times 5 is \(index * 5)")
}
for?_?in?1...5?{
? ? 可以用下劃線忽略當(dāng)前值
}
2.字典通過(guò)元祖返回
3.do while循環(huán)變成repeat
repeat {
? ? statements
} while condition
4.switch不需要break
let someCharacter: Character = "z"
switch someCharacter {
case "a":
? ? print("The first letter of the alphabet")
case "z":
? ? print("The last letter of the alphabet")
default:
? ? print("Some other character")
}
5.switch case的body不能為空
6.case可以帶區(qū)間
let approximateCount = 62
let countedThings = "moons orbiting Saturn"
var naturalCount: String
switch approximateCount {
case 0:
? ? naturalCount = "no"
case 1..<5:
? ? naturalCount = "a few"
case 5..<12:
? ? naturalCount = "several"
case 12..<100:
? ? naturalCount = "dozens of"
case 100..<1000:
? ? naturalCount = "hundreds of"
default:
? ? naturalCount = "many"
}
print("There are \(naturalCount) \(countedThings).")
7.case的元祖表示
let somePoint = (1, 1)
switch somePoint {
case (0, 0):
? ? print("(0, 0) is at the origin")
case (_, 0):
? ? print("(\(somePoint.0), 0) is on the x-axis")
case (0, _):
? ? print("(0, \(somePoint.1)) is on the y-axis")
case (-2...2, -2...2):
? ? print("(\(somePoint.0), \(somePoint.1)) is inside the box")
default:
? ? print("(\(somePoint.0), \(somePoint.1)) is outside of the box")
}
8.case加額外條件
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
? ? print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
? ? print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
? ? print("(\(x), \(y)) is just some arbitrary point")
}
9.case ?fallthrough貫穿
fallthrough關(guān)鍵字不會(huì)檢查它下一個(gè)將會(huì)落入執(zhí)行的 case 中的匹配條件。fallthrough簡(jiǎn)單地使代碼繼續(xù)連接到下一個(gè) case 中的代碼
10.while加標(biāo)簽
gameLoop: while square != finalSquare {
? ? diceRoll += 1
? ? if diceRoll == 7 { diceRoll = 1 }
? ? switch square + diceRoll {
? ? case finalSquare:
? ? ? ? // diceRoll will move us to the final square, so the game is over
? ? ? ? break gameLoop
? ? case let newSquare where newSquare > finalSquare:
? ? ? ? // diceRoll will move us beyond the final square, so roll again
? ? ? ? continue gameLoop
? ? default:
? ? ? ? // this is a valid move, so find out its effect
? ? ? ? square += diceRoll
? ? ? ? square += board[square]
? ? }
}
print("Game over!")
11.guard與if的區(qū)別
像if語(yǔ)句一樣,guard的執(zhí)行取決于一個(gè)表達(dá)式的布爾值。我們可以使用guard語(yǔ)句來(lái)要求條件必須為真時(shí),以執(zhí)行guard語(yǔ)句后的代碼。不同于if語(yǔ)句,一個(gè)guard語(yǔ)句總是有一個(gè)else從句,如果條件不為真則執(zhí)行else從句中的代碼。
guard let name = person["name"] else {
? ? return
}
12.檢測(cè) API 可用性
Swift內(nèi)置支持檢查 API 可用性,這可以確保我們不會(huì)在當(dāng)前部署機(jī)器上,不小心地使用了不可用的API。
if #available(iOS 10, macOS 10.12, *) {
? ? // 在 iOS 使用 iOS 10 的 API, 在 macOS 使用 macOS 10.12 的 API
} else {
? ? // 使用先前版本的 iOS 和 macOS 的 API
}
在它一般的形式中,可用性條件使用了一個(gè)平臺(tái)名字和版本的列表。平臺(tái)名字可以是iOS,macOS,watchOS和tvOS——請(qǐng)?jiān)L問(wèn)聲明屬性來(lái)獲取完整列表。除了指定像 iOS 8的主板本號(hào),我們可以指定像iOS 8.3 以及 macOS 10.10.3的子版本號(hào)。
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/huoran1120/p/6118409.html
總結(jié)
以上是生活随笔為你收集整理的Swift--控制流与oc不同的地方的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到拜菩萨是什么意思
- 下一篇: Atitit 数据存储的分组聚合 gr