Swift 流程控制(四)

2020-12-08  本文已影响0人  奋斗的小老鼠

for-in 循环

for i in 0...3 {
    print(i)
}
for c in "Hello,World" {
    print(c)
}
let names = ["zhangsan","lisi","wangwu","zhaoliu"]
for name in names {
    print(name)
}
let base = 3
let power = 5
var answer = 1
for _ in 1...power {
    answer *= base
}
print("\(base) to the power of \(power) is \(answer)")
输出
3 to the power of 5 is 243

for-in 遍历字典

let numberOfLegs = ["spider":8,"ant":6,"cat":4]

for (animalName,legCount) in numberOfLegs {
    print("\(animalName) has \(legCount)")
}

for t in numberOfLegs {
    print("\(t.0) has \(t.1) legs")
}
输出
ant has 6
cat has 4
spider has 8
ant has 6 legs
cat has 4 legs
spider has 8 legs

for-in 分段区间

let minuteInterval = 10
////开区间
for tickMark in stride(from: 0, to: 50, by: minuteInterval) {
    print(tickMark)
}
输出
0
10
20
30
40
////闭区间
for tickMark2 in stride(from: 0, through: 50, by: minuteInterval){
    print(tickMark2)
}
输出
0
10
20
30
40
50

while 循环

var count = 0
repeat {
    print(count)
    count += 1
} while count < 5

switch

char c = "z"
switch (c) {
    case "a":
        print("The first letter of alphabet");
        break;
    case "z":
        print("The last letter of alphabet");
        break;
}

而 swift 报错,必须添加默认的defaule

let someCharacter: Character = "z"
switch someCharacter {
case "a":
    print("The first letter of alphabet")
case "z":
    print("The last letter of alphabet")
}
没有隐式贯穿
let anotherCharacter:Character = "a"
switch anotherCharacter {
case "a","A":
    print("The letter A")
default:
    print("Not the letter A")
}
输出
The letter A
区间匹配
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 ara \(naturalCount) \(countedThings).")
输出
There ara dozens of moons orbiting Saturn.
元祖匹配
let somePoint = (1,1)
switch somePoint {
case (0,0):
    print("(0,0) is at the arigin")
case (_,0):
    print("(\(somePoint.0),0) is on the x-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
绑定值
let anotherPoint = (0,2)
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 an y value of \(y)")
case let(x,y):
    print("somewhere else at (\(x),\(y))")
default:
    print("没有匹配到")
}
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")
default:
    print("其他")
}
复合匹配
let someCharacter:Character = "g"
switch someCharacter {
case "a","e","i","o","u":
    print("\(someCharacter) is a vowel")
case "b","c","d","f","g","h","j","k","l",
     "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")
}
复合匹配 - 值绑定
let stillAnotherPoint = (9,0)
switch stillAnotherPoint {
case (let distance,0),(0,let distance):
    print("On an axis,\(distance) from the origin")
default:
    print("Not on an axis")
}

控制转移

continue
break
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)
输出
The number 5 is a prime number, and also an integer.

语句标签

guard 和检查 API 可用性

guard
func fooGuard(x: Int?) {
    guard let x = x where x > 0 else {
        // 变量不符合条件判断时,执行下面代码
        return
    }
    
    // 使用x
    x.description
}
  • guard 是对你所期望的条件做检查,而非不符合你期望的。又是和assert很相似。如果条件不符合,guard的else语句就运行,从而退出这个函数。
  • 如果通过了条件判断,可选类型的变量在guard语句被调用的范围内会被自动的拆包 - 这个例子中该范围是fooGuard函数内部。这是一个很重要,却有点奇怪的特性,但让guard语句十分实用。
  • 对你所不期望的情况早做检查,使得你写的函数更易读,更易维护。
检查API的可用性
上一篇下一篇

猜你喜欢

热点阅读