Swift Cheat Sheet [2] - Control Flow
发布时间:2020-12-14 01:41:44 所属栏目:百科 来源:网络整理
导读:If条件语句 if else let number = 100if number 10 { print("The number is small")} else if number 100 { print("The number is pretty big")} else { print("The number is between 10 and 100")} if else + for in let individualScores = [75,43,103,87
If条件语句if elselet number = 100 if number < 10 { print("The number is small") } else if number > 100 { print("The number is pretty big") } else { print("The number is between 10 and 100") } if else + for inlet individualScores = [75,43,103,87,12] var teamScore = 0 for score in individualScores { if score > 50 { teamScore += 3 } else { teamScore += 1 } } print(teamScore) 使用可选绑定var optionalName: String? var greeting = "Hello!" if let name = optionalName { greeting = "Hello,(name)" }else { print("optionalName is nil") } 在if条件判断语句中使用where关键字var optionalHello: String? = "Hello" if let hello = optionalHello where hello.hasPrefix("H"),let name = optionalName { greeting = "(hello),(name)" print("greeting:(greeting)") }else { //跳到这里,因为hello还没有被复制,它没有“H”前缀,仅当where模式匹配成功,if条件语句才执行。 } Switch一条case分支可匹配多个模式let vegetable = "red pepper" switch vegetable { case "celery": let vegetableComment = "Add some raisins and make ants on a log." case "cucumber","watercress": let vegetableComment = "That would make a good tea sandwich." case let x where x.hasSuffix("pepper"): let vegetableComment = "Is it a spicy (x)?" default://必须有default分支 let vegetableComment = "Everything tastes good in soup." } 不存在隐式的贯穿下面的代码会有编译错误! let anotherCharacter: Character = "a" switch anotherCharacter { case "a": case "A": print("The letter A") default: print("Not the letter A") } // this will report a compile-time error 区间匹配使用闭区间操作符 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).") // 输出 "There are dozens of moons orbiting Saturn." 使用元组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") } // 输出 "(1,1) is inside the box"
case分支中使用值绑定let anotherPoint = (2,0) switch anotherPoint { case (let x,0): print("on the x-axis with an x value of (x)") case (0,let y): print("on the y-axis with a y value of (y)") case let (x,y): print("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: 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") } // 输出 "(1,-1) is on the line x == -y" For循环使用For-Invar firstForLoop = 0 for i in 0..<4 { firstForLoop += i } print(firstForLoop) var secondForLoop = 0 for _ in 0...4 { secondForLoop += 1 } print(secondForLoop) 使用下划线_替代循环变量名如果你不需要知道区间内每一项的值,你可以使用下划线(_)替代变量名来忽略对值的访问: let base = 3 let power = 10 var answer = 1 for _ in 1...power { answer *= base } print("(base) to the power of (power) is (answer)") // 输出 "3 to the power of 10 is 59049" code2: var secondForLoop = 0 for _ in 0...4 { secondForLoop += 1 } print(secondForLoop) //输出5 遍历数组元素let names = ["Anna","Alex","Brian","Jack"] for name in names { print("Hello,(name)!") } // Hello,Anna! // Hello,Alex! // Hello,Brian! // Hello,Jack! 遍历字典的键值对字典元素的遍历顺序和插入顺序可能不同 let numberOfLegs = ["spider": 8,"ant": 6,"cat":4] for (animalName,legCount) in numberOfLegs{ print("(animalName)s have (legCount) legs") } // ants have 6 legs // cats have 4 legs // spiders have 8 legs 使用条件判断和递增方法的标准 C 样式for循环for var index = 0; index < 3; ++index { print("index is (index)") } // index is 0 // index is 1 // index is 2 While循环whilevar count = 1 while count < 3 { println("count is (count)") ++count } // count is 1 // count is 2 count = 1 while count < 1 { println("count is (count)") ++count } repeat-whilevar count = 1 repeat { print("count is (count)") ++count } while count < 3 // count is 1 // count is 2 count = 1 repeat { print("count is (count)") ++count } while count < 1 // count is 1 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |