//一、for-in循环
//可以使用for-in迭代数组、区间、字符串等序列
//使用for-in迭代数组
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
print("Hello, \(name)!")
}
//使用for-in迭代字典
//通过(key-value)元组得到字典键值对,访问元组的元素得到字典的健和值
// cats have 4 legs
// ants have 6 legs
// spiders have 8 legs
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs")
}
//使用for-in遍历数字区间
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
//在上面的例子中,index是一个常量,它的值在循环的每次迭代开始时自动设置。
//因此,index在使用之前不必声明。它只是通过包含在循环声明中的隐式声明,不需要let声明关键字
//如果不需要使用序列值,可以通过使用下划线取代这个变量名
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}
// Prints "3 to the power of 10 is 59049"
print("\(base) to the power of \(power) is \(answer)")
//使用半开区间运算符(..<),包括下限但不包括上限
let minutes = 60
for tickMark in 0..
//使用该stride(from:to:by:)方法跳过不需要的标记
let minuteInterval = 5
for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
// render the tick mark every 5 minutes (0, 5, 10, 15 ... 45, 50, 55)
print(tickMark)
}
//封闭区间也可以使用stride(from:through:by:)
let hours = 12
let hourInterval = 3
for tickMark in stride(from: 3, through: hours, by: hourInterval) {
// render the tick mark every 3 hours (3, 6, 9, 12)
print(tickMark)
}
//二、while
//Swift 提供了两种while循环:
//while 在每次循环开始时评估其条件
//repeat-while在每次循环结束时评估其条件
//1.while
//如果条件为true,则重复一组语句,直到条件变为false。
//while循环的一般形式:
while condition {
statements
}
示例代码:
//let finalSquare = 25
//var board = [Int](repeating: 0, count: finalSquare + 1)
//
//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
//while square < finalSquare {
// // roll the dice
// diceRoll += 1
// if diceRoll == 7 {
// diceRoll = 1
// }
//
// // move by the rolled amount
// square += diceRoll
// if square < board.count {
// // if we're still on the board, move up or down for a snake or a ladder
// square += board[square]
// }
//}
//print("Game over!")
//2.Repeat-While
//类似于其他C语言的do-while
//repeat-while循环的一般形式:
repeat {
statements
} while condition
//三、条件语句
//根据特定条件执行不同的代码块通常很有用
//您可能希望当发生错误时运行一段额外的代码,或者在值变得过高或过低时显示一条消息。为此,您需要将部分代码设置为条件。
//1.if
//在最简单的形式中,if语句只有一个if条件。仅当该条件为true时,才执行一组语句
var temperatureInFahrenheit = 30
// Prints "It's very cold. Consider wearing a scarf."
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
}
//当if语句的条件为false时,使用else分句执行条件不成立时的内容
temperatureInFahrenheit = 40
// Prints "It's not that cold. Wear a t-shirt."
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
} else {
print("It's not that cold. Wear a t-shirt.")
}
//根据条件分句,可以把多个if语句串联起来
temperatureInFahrenheit = 90
// Prints "It's really warm. Don't forget to wear sunscreen."
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.")
}
//如果条件不需要完整,最后一个else语句不是必须的
//2.switch
//switch语句在几种可能的匹配模式中,根据第一个成功匹配的的模式,执行合适的代码块
//以下是switch的形式:
//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
//}
//每一个switch陈述都必须详尽无遗。也就是说,所考虑的类型的每个可能值都必须与其中一种switch情况匹配。
//如果不能为每个可能的值提供一个case,您可以定义一个默认case来涵盖任何未明确处理的值,此默认情况由default关键字,并且default关键字必须始终出现在最后。
let someCharacter: Character = "z"
// Prints "The last letter of the alphabet"
switch someCharacter {
case "a":
print("The first letter of the alphabet")
case "z":
print("The last letter of the alphabet")
default:
print("Some other character")
}
//与C和Objective-C中的switch语句相比,Swift中的switch语句匹配上第一个case后立即完成其执行,而不需要显式使用break语句
//每个case必须包含可执行的内容,如果case内容为空,会报编译错误
//let anotherCharacter: Character = "a"
//switch anotherCharacter {
//case "a": // Invalid, the case has an empty body
//case "A":
// print("The letter A")
//default:
// print("Not the letter A")
//}
//在上面的示例中,如果要让case匹配a和A,可以将a和A组合在一起,用逗号进行分割
let anotherCharacter: Character = "a"
// Prints "The letter A"
switch anotherCharacter {
case "a", "A":
print("The letter A")
default:
print("Not the letter A")
}
//3.区间匹配
//switch可以检查案例中的值是否包含在间隔中
let approximateCount = 62
let countedThings = "moons orbiting Saturn"
let 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"
}
// Prints "There are dozens of moons orbiting Saturn."
print("There are \(naturalCount) \(countedThings).")
//4.元组
//您可以使用同一switch语句来测试元组中的多个值。元组的每个元素都可以用不同的值或间隔值进行测试。
//使用下划线字符(_)(也称为通配符模式)来匹配任何可能的值
let somePoint = (1, 1)
// Prints "(1, 1) is inside the box"
switch somePoint {
case (0, 0):
print("\(somePoint) is at the origin")
case (_, 0):
print("\(somePoint) is on the x-axis")
case (0, _):
print("\(somePoint) is on the y-axis")
case (-2...2, -2...2):
print("\(somePoint) is inside the box")
default:
print("\(somePoint) is outside of the box")
}
//5.值绑定
//一个switch case,可以将一个或多个值用匹配的常量或变量进行民命,然后在case的语句体中进行使用
let anotherPoint = (2, 0)
// Prints "on the x-axis with an x value of 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 a y value of \(y)")
case let (x, y):
print("somewhere else at (\(x), \(y))")
}
//6.Where
//一个switch case语句,可以使用where子句来检查附加条件
let yetAnotherPoint = (1, -1)
// Prints "(1, -1) is on the line x == -y"
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")
}
//7.复合case
//共享同一个case体的多个switch case,可以通过多个匹配模式组合,每个模式之间用逗号进行分割
let someCharacter: Character = "e"
Prints "e is a vowel"
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) isn't a vowel or a consonant")
}
//复合case可以包含值绑定
//复合case的所有模式都必须包含相同的一组值绑定,并且每一个绑定都不行是相同类型的值
let stillAnotherPoint = (9, 0)
// Prints "On an axis, 9 from the origin"
switch stillAnotherPoint {
case (let distance, 0), (0, let distance):
print("On an axis, \(distance) from the origin")
default:
print("Not on an axis")
}
//四、控制转移语句
//控制转移语句通过将控制从一段代码转移到另一段代码来改变代码的执行顺序
//Swift 有五个控制转移语句:
continue
break
fallthrough
return
throw
//1.continue
//该continue语句告诉循环停止正在执行的操作,并重新开始循环的下一次迭代
//以下示例从小写字符串中删除所有元音和空格以创建一个神秘的拼图短语:
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
let charactersToRemove: [Character] = ["a", "e", "i", "o", "u", " "]
for character in puzzleInput {
if charactersToRemove.contains(character) {
continue
}
puzzleOutput.append(character)
}
print(puzzleOutput)
// Prints "grtmndsthnklk"
//2.break
//break语句立即结束整个控制流语句的执行
//break语句可以用在sitch语句或者循环体中,用于提前结束执行的语句
// 循环中的break,用于停止本次循环
// switch中,break语句不是必须的,用break语句,可以让case中的内容为空
//下面的示例是一个Character值代表四种语言之一的数字符号
let numberSymbol: Character = "三" // Chinese symbol 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 couldn't be found for \(numberSymbol).")
}
// Prints "The integer value of 三 is 3."
//3.Fallthrough
//在 Swift 中,switch语句不会从每个 case 的底部落到下一个 case
// 如果需要在switch语句中,像C语言的case一样落到下一个case,
// 可以使用fallthough关键词
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
description += " bbbbbbb "
default:
description += " an integer."
}
print(description)
// Prints "The number 5 is a prime number, and also an integer."
//备注
//fallthrough关键字不会检查case条件,使用了fallthrough关键字
//代码直接执行到下一个case语句块
//4.标签语句
//带标签的语句通过将标签放置在与语句的介绍者关键字相同的行上来表示,后面跟一个冒号
//格式如下:
label name: while condition {
statements
}
示例:
let finalSquare = 25
var board = [Int](repeating: 0, count: finalSquare + 1)
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 {
diceRoll += 1
if diceRoll == 7 { diceRoll = 1 }
switch square + diceRoll {
case finalSquare:
// diceRoll will move us to the final square, so the game is over
break gameLoop
case let newSquare where newSquare > finalSquare:
print("current diceRoll is \(diceRoll)")
// diceRoll will move us beyond the final square, so roll again
continue gameLoop
default:
// this is a valid move, so find out its effect
square += diceRoll
square += board[square]
}
}
print("Game over!")
//如上面的示例代码,使用标签语句可以在case中直接退出循环
//5.提前退出
//guard语句,就像一个if语句,根据表达式的布尔值结果执行语句
//您可以要求条件必须为真来使用guard语句,才能执行该语句之后的代码
//与if语句不同,guard语句总是有一个else子句——如果条件不为真,else子句中的代码就会被执行
func greet(person: [String: String]) {
guard let name = person["name"] else {
return
}
print("Hello \(name)!")
guard let location = person["location"] else {
print("I hope the weather is nice near you.")
return
}
print("I hope the weather is nice in \(location).")
}
greet(person: ["name": "John"])
// Prints "Hello John!"
// Prints "I hope the weather is nice near you."
greet(person: ["name": "Jane", "location": "Cupertino"])
// Prints "Hello Jane!"
// Prints "I hope the weather is nice in Cupertino."
//五、检查 API 可用性
//Swift内置支持检查API可用性,这可确保您不会意外使用在给定部署目标上不可用的API
//编译器使用SDK中的可用性信息来验证您的代码中使用的所有API,在您的项目指定的部署目标上是否可用
//如果您尝试使用不可用的API,Swift会在编译时报告错误
if #available(iOS 10, macOS 10.12, *) {
// Use iOS 10 APIs on iOS, and use macOS 10.12 APIs on macOS
print("Use iOS 10 APIs on iOS, and use macOS 10.12 APIs on macOS")
} else {
// Fall back to earlier iOS and macOS APIs
print("Fall back to earlier iOS and macOS APIs")
}
//除了指定 iOS 8 或 macOS 10.10 等主要版本号外
//您还可以指定 iOS 11.2.6 和 macOS 10.13.3 等次要版本号
//检查API可用性的代码格式如下:
if #available(platform name version, ..., *) {
statements to execute if the APIs are available
} else {
fallback statements to execute if the APIs are unavailable
}