在下面的例子是打印从1到6的数, 1...6 1是开头 6是结尾,开头和结尾都包括在其中。
第一个例子:
for index in 1...6{ print("index = \(index)") }
打印:
index = 1 index = 2 index = 3 index = 4 index = 5 index = 6
第二个例子:
这个例子相当于普通的for循环。 for(int i = 1;i < 10;i++)
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
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
for character in "Dog!".characters { print(character) }
打印:
D o g !
先执行循环,知道条件为假就结束,和do-while一样。
repeat { statements } while condition
这个没什么好讲的。
var temperatureInFahrenheit = 90 if temperatureInFahrenheit <= 32 { print("It's very cold. Consider wearing a scarf.") } else if temperatureInFahrenheit >= 86 { print("It's really warm. Don't forget to wear sunscreen.") } else { print("It's not that cold. Wear a t-shirt.") }
打印:
It's really warm. Don't forget to wear sunscreen.
可以多个条件,可以不要break。
switch some value to consider { case value 1: respond to value 1 case value 2, value 3: respond to value 2 or 3 default: otherwise, do something else }
let someCharacter: Character = "e" switch someCharacter { case "a", "e", "i", "o", "u": print("\(someCharacter) is a vowel") case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z": print("\(someCharacter) is a consonant") default: print("\(someCharacter) is not a vowel or a consonant") } // prints "e is a vowel"
let anotherCharacter: Character = "a" switch anotherCharacter { case "a": print("The letter a") case "A": print("The letter A") default: print("Not the letter A") } // this will report a compile-time error
元组的switch - 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") } // prints "(1, 1) is inside the box"
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))") } // prints "on the x-axis with an x value of 2"
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") } // prints "(1, -1) is on the line x == -y"
let puzzleInput = "great minds think alike" var puzzleOutput = "" for character in puzzleInput.characters { switch character { case "a", "e", "i", "o", "u", " ": continue default: puzzleOutput.append(character) } } print(puzzleOutput) // prints "grtmndsthnklk"
打印的时case之外的字符串
let numberSymbol: Character = "三" // Simplified Chinese for the number 3 var possibleIntegerValue: Int? switch numberSymbol { case "1", "١", "一", "๑": possibleIntegerValue = 1 case "2", "٢", "二", "๒": possibleIntegerValue = 2 case "3", "٣", "三", "๓": possibleIntegerValue = 3 case "4", "٤", "四", "๔": possibleIntegerValue = 4 default: break } if let integerValue = possibleIntegerValue { print("The integer value of \(numberSymbol) is \(integerValue).") } else { print("An integer value could not be found for \(numberSymbol).") } // prints "The integer value of 三 is 3."
let integerToDescribe = 5 var description = "The number \(integerToDescribe) is" switch integerToDescribe { case 2, 3, 5, 7, 11, 13, 17, 19: description += " a prime number, and also" fallthrough default: description += " an integer." } print(description) // prints "The number 5 is a prime number, and also an integer."
检查API的有效性
if #available(platform name version, ..., *) { statements to execute if the APIs are available } else { fallback statements to execute if the APIs are unavailable }
if #available(iOS 9, OSX 10.10, *) { // Use iOS 9 APIs on iOS, and use OS X v10.10 APIs on OS X } else { // Fall back to earlier iOS and OS X APIs }