Swift Control Flow控制流

前端之家收集整理的这篇文章主要介绍了Swift Control Flow控制流前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一.For-in

1. ...

在下面的例子是打印从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

2.数组的遍历

let names = ["Anna","Alex","Brian","Jack"]

for name in names {
    print("Hello,\(name)!")
}
打印:
Hello,Anna!
Hello,Alex!
Hello,Brian!
Hello,Jack!

3.字典的遍历

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

4.字符串的遍历

for character in "Dog!".characters {
    print(character)
}
打印:

D
o
g
!

二、Repeat-While

先执行循环,知道条件为假就结束,和do-while一样。

repeat {
    statements
} while condition

三、if

这个没什么好讲的。

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.

四、Switch

可以多个条件,可以不要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
}

例子1:

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"

例子2:

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

例子3:

元组的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"

例子4:

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"

例子5: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")
}
// prints "(1,-1) is on the line x == -y"

例子6:Continue

let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput.characters {
    switch character {
    case "a","u"," ":
        continue
    default:
        puzzleOutput.append(character)
    }
}
print(puzzleOutput)
// prints "grtmndsthnklk"
打印的时case之外的字符串

例子7:break

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."

例子8:fallthrough

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."

例子9:#available

检查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
}

详细原文请查看

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-ID120

猜你在找的Swift相关文章