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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

swift 运算符和控制流程

發布時間:2025/5/22 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 swift 运算符和控制流程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

閉區間運算符

閉區間運算符(a...b)定義一個包含從a到b(包括a和b)的所有值的區間,只能是數字

for index in 1...5 {println("\(index) * 5 = \(index * 5)") }var names = ["Anna", "Alex", "Brian", "Jack"] names[2...3] = ["a","b"] //不能添加

半閉區間

半閉區間(a..b)定義一個從a到b但不包括b的區間。方便取數組下標

let names = ["Anna", "Alex", "Brian", "Jack"] let count = names.count for i in 0..count {println("第 \(i + 1) 個人叫 \(names[i])") }for i in 0...count-1{println("第 \(i + 1) 個人叫 \(names[i])") }

閉區間和半閉區間代替了傳統的.for循環.使用i++的形式,還可以用于再數組上

fon-in

for-in用來遍歷一個區間(range),序列(sequence),集合(collection),系列(progression)里面所有的元素執行一系列語句。

//區間 for index in 1...5 {println("\(index) times 5 is \(index * 5)") } //數組 let names = ["Anna", "Alex", "Brian", "Jack"] for name in names {println("Hello, \(name)!") } //字典 let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4] for (animalName, legCount) in numberOfLegs {println("\(animalName)s have \(legCount) legs") } //字符 for character in "Hello" {println(character) }

for-in中index是一個每次循環遍歷開始時被自動賦值的常量。這種情況下,index在使用前不需要聲明,只需要將它包含在循環的聲明中 ,就可以對其進行隱式聲明,而無需使用let關鍵字聲明。
index常量只存在于循環的生命周期里。如果你想在循環完成后訪問index的值,又或者想讓index成為一個變量而不是常量, 你必須在循環之前自己進行聲明。

for循環

在初始化表達式中聲明的常量和變量(比如var index = 0)只在for循環的生命周期里有效。如果想在循環結束后訪問index的值,你必須要在循環生命周期開始前聲明index。

for initialization; condition; increment { statements }

等同于

initialization while condition { statements increment }

switch

switch當匹配后,不會繼續執行下一個case,會終止switch語句,所以不需要break語句.如果想要貫穿至特定的 case 分支中,請使用fallthrough貫穿語句
每一個 case 分支都必須包含至少一條語句。代替可以使用","匹配多個case,為同一個值

let someCharacter: Character = "e" switch someCharacter { case "a", "e", "i", "o", "u":println("\(someCharacter) is a vowel") case "b", "c", "d",:println("\(someCharacter) is a consonant") default:println("\(someCharacter) is not a vowel or a consonant") }//case 條件可以是區間,查找一個數字是否在一個區間 switch count { case 0:naturalCount = "no" case 1...3:naturalCount = "a few" case 4...9:naturalCount = "several" case 10...99:naturalCount = "tens of" case 100...999:naturalCount = "hundreds of" case 1000...999_999:naturalCount = "thousands of" default:naturalCount = "millions and millions of" }使用元組在同一個switch語句中測試多個值。元組中的元素可以是值,也可以是區間。另外,使用下劃線(_)來匹配所有可能的值。 let somePoint = (1, 1) switch somePoint { case (0, 0):println("(0, 0) is at the origin") case (_, 0):println("(\(somePoint.0), 0) is on the x-axis") case (0, _):println("(0, \(somePoint.1)) is on the y-axis") case (-2...2, -2...2):println("(\(somePoint.0), \(somePoint.1)) is inside the box") default:println("(\(somePoint.0), \(somePoint.1)) is outside of the box") }//值綁定 let anotherPoint = (2, 0) switch anotherPoint { case (let x, 0):println("on the x-axis with an x value of \(x)") case (0, let y):println("on the y-axis with a y value of \(y)") case let (x, y):println("somewhere else at (\(x), \(y))") } // 輸出 "on the x-axis with an x value of 2"//使用where let yetAnotherPoint = (1, -1) switch yetAnotherPoint { case let (x, y) where x == y:println("(\(x), \(y)) is on the line x == y") case let (x, y) where x == -y:println("(\(x), \(y)) is on the line x == -y") case let (x, y):println("(\(x), \(y)) is just some arbitrary point") } // 輸出 "(1, -1) is on the line x == -y"

Label

使用break和continue在多重循環或者switch嵌套中跳轉

gameLoop: while square != finalSquare {if ++diceRoll == 7 { diceRoll = 1 }switch square + diceRoll {case finalSquare:// 到達最后一個方塊,游戲結束break gameLoopcase let newSquare where newSquare > finalSquare:// 超出最后一個方塊,再擲一次骰子continue gameLoopdefault:// 本次移動有效square += diceRollsquare += board[square]} } println("Game over!")

轉載于:https://www.cnblogs.com/zhepama/p/3855752.html

總結

以上是生活随笔為你收集整理的swift 运算符和控制流程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。