//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
//For循环
//For in
for index in 1...5{
print(index)
}
//如果你不需要知道区间内每一项的值,你可以使用下划线(_)替代变量名来忽略对值的访问
let power = 6
for _ in 1...power {
print("++++++")
}
//元组遍历
let numberOfLegs = ["spider": 8, "ant": 6]
for (code,cont) in numberOfLegs{
print(code)
print(cont)
}
//let http = (4,"rrr")
//http.0
/**
* switch
区间匹配
case 分支的模式也可以是一个值的区间
*/
let count = 3_000_000_000_000
let countedThings = "stars in the Milky Way"
var naturalCount: String
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"
}
print("There are \(naturalCount) \(countedThings).")
/**
* 元组
你可以使用元组在同一个switch语句中测试多个值。元组中的元素可以是值,也可以是区间。另外,使用下划线(_)来匹配所有可能的值
*/
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")
}
/**
* 值绑定
case 分支的模式允许将匹配的值绑定到一个临时的常量或变量,
这些常量或变量在该 case 分支里就可以被引用了——这种行为被称为值绑定(value binding)。
*/
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))")
}
/**
* Where
case 分支的模式可以使用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")
}
/**
* 控制转移语句
控制转移语句改变你代码的执行顺序,通过它你可以实现代码的跳转。Swift有四种控制转移语句。
continue
break
fallthrough
return
*/
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput.characters {
switch character {
case "a", "e", "i", "o", "u", " ":
print("1")
continue
default:
puzzleOutput.append(character)
}
}
let numberSymbol: Character = "三" // 简体中文里的数字 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).")
}
/**
* 贯穿
fallthrough
注意:
fallthrough关键字不会检查它下一个将会落入执行的 case 中的匹配条件。
fallthrough简单地使代码执行继续连接到下一个 case 中的执行代码,这和 C 语言标准中的switch语句特性是一样的
*/
//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
case 1:
description += "34"
default:
description += " an integer."
}
print(description)
/**
* 带标签的语句
*/
let finalSquare = 25
var board = [Int](count: finalSquare + 1, repeatedValue: 0)
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
var square = 0
var diceRoll = 0
gameLoop: while square != finalSquare {
if ++diceRoll == 7 { diceRoll = 1 }
switch square + diceRoll {
case finalSquare:
// 到达最后一个方块,游戏结束
break gameLoop
case let newSquare where newSquare > finalSquare:
// 超出最后一个方块,再掷一次骰子
continue gameLoop
default:
// 本次移动有效
square += diceRoll
square += board[square]
}
}
print("Game over!")
/*
注意:
如果上述的break语句没有使用gameLoop标签,那么它将会中断switch代码块而不是while循环体。
使用gameLoop标签清晰的表明了break想要中断的是哪个代码块。
同时请注意,当调用continue gameLoop去跳转到下一次循环迭代时,
这里使用gameLoop标签并不是严格必须的。因为在这个游戏中,只有一个循环体,
所以continue语句会影响到哪个循环体是没有歧义的。然而,continue语句使用gameLoop标签也是没有危害的。
这样做符合标签的使用规则,同时参照旁边的break gameLoop,能够使游戏的逻辑更加清晰和易于理解。
*/