日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Swift--控制流与oc不同的地方

發(fā)布時(shí)間:2023/11/29 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Swift--控制流与oc不同的地方 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)名字可以是iOSmacOSwatchOStvOS——請(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。