控制流

Swift包含很多控制流语句:while循环、if、guard、switch和for-in循环。

For-In循环

for-in循环用于迭代序列,如数组遍历、数字范围、字符串中的字符。

下面为for-in循环遍历数组:

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
// spiders have 8 legs
// cats have 4 legs

遍历数字范围:

for index in 1...5 {print("\(index) times 5 is \(index * 5)")
}
// 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

若不需要序列中的值时,使用下划线代替变量名来忽略这些值:

let base = 3
let power = 10
var answer = 1
for _ in 1...power {answer *= base
}
print("\(base) to the power of \(power) is \(answer)")

遍历半开数字范围:

let minutes = 60
for tickMark in 0..<minutes {// render the tick mark each minute (60 times)
}

使用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)
}

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)
}

While循环

Swift有两种while循环:

  • 每次循环开始时判断循环条件的while循环
  • 每次循环结束时判断循环条件的repeat-while循环

While

while循环语法:

while condition {statements}

Repeat-While

Swift的repeat-while循环与其他语言的do-while循环类似。

repeat-while循环语法:

repeat {statements} while condition

条件语句

Swift提供两种条件语句:ifswitch语句。if判断简单条件,可能结果较少;switch适合复杂条件且可能情况较多的情况,常用于模式匹配。

If

Swift中的if语句可忽略判断条件的括号,但执行语句的大括号不可或缺:

var temperatureInFahrenheit = 30
if temperatureInFahrenheit <= 32 {print("It's very cold. Consider wearing a scarf.")
}
// Prints "It's very cold. Consider wearing a scarf."

if-else语句:

temperatureInFahrenheit = 40
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
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.")
}

Switch

switch语句比较多个匹配模式,且能匹配任意类型值,语法如下:

switch some value to consider {case value 1:respond to value 1case value 2,value 3:respond to value 2 or 3default:otherwise, do something else}

switch语句必须列出所有可能情况。

下面例子匹配单个小写字符:

let someCharacter: Character = "z"
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")
}

Swift的switch语句不需要在每个语句块最后显式使用break语句(也可使用)。当匹配某个条件时,之间结束执行整个switch语句。

switch语句的每个条件必须只要包含一个可执行语句,否则会抛出编译错误:

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")
}
// This will report a compile-time error.

要将两个判断条件合并,使用逗号隔开:

let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a", "A":print("The letter A")
default:print("Not the letter A")
}

注意

要想显示跳过某次判断条件,进入下个条件,使用fallthrough关键字。

间隔匹配

使用间隔匹配可在一条判断语句中匹配多个值:

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"
}
print("There are \(naturalCount) \(countedThings).")
// Prints "There are dozens of moons orbiting Saturn."

匹配元组

使用元组可在一条switch语句中匹配复合值,并使用下划线匹配元组中某个元素的所有可能值。

当已经匹配某个条件时,之后的所有条件都将被忽略。
let somePoint = (1, 1)
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")
}
// 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"

当某个条件正好匹配所有可能的剩余情况时,则default语句可忽略。

Where

使用where语句对某个case添加额外条件:

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"

复合case

使用逗号在一个case匹配多个条件,条件过长时可多行显示:

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 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")
}
// Prints "On an axis, 9 from the origin"

控制转移语句

Swift有五种控制转移语句:

  • continue
  • break
  • fallthrough
  • return
  • throw

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} else {puzzleOutput.append(character)}
}
print(puzzleOutput)
// Prints "grtmndsthnklk"

Break

break语句立即结束执行整个控制流语句。

用于循环语句的break

当在循环语句中使用break时,break会立即结束执行整个循环语句。

用于switch语句的break

当在switch语句中使用break时,break会立即结束执行整个switch

Swift的switch语句中不用显示使用break跳出匹配,但可以在某个case中可以插入break结束匹配以使意图明确。

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 could not be found for \(numberSymbol).")
}
// Prints "The integer value of 三 is 3."

Fallthrough

Swift中,switch语句在匹配到某个case条件后会忽略之后所有case,不会自动下一个case。若需要像C语言那样跳入下个case中,可使用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."

注意

fallthrough关键字不会检查下个进入的case判断条件,只是简单地跳入下一个case并执行内部代码,与C语言中的switch语句相似。

标签语句

当使用循环或条件嵌套语句时,需要明确说明要使用breakcontinue语句来结束哪层控制语句,这时使用标签语句对需要提前结束的控制语句做标签,然后在breakcontinue语句后接上已声明的标签名来结束带相同标签的语句。语法如下:

label name: while condition {statements}

guard语句

guard语句是if语句的变体,通过将判断条件的主体执行代码放在guard语句末尾以使代码意图更加清晰。

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导致意外错误。

当尝试使用不可用API时,Swift会报告编译错误。

if #available(iOS 10, macOS 10.12, *) {// Use iOS 10 APIs on iOS, and use macOS 10.12 APIs on macOS
} else {// Fall back to earlier iOS and macOS APIs
}

语法如下:

if #available(platform name version, ..., *) {statements to execute if the APIs are available} else {fallback statements to execute if the APIs are unavailable}

转载于:https://www.cnblogs.com/keqipu/p/7620423.html

Swift教程之控制流相关推荐

  1. 中文版 Apple 官方 Swift 教程《The Swift Programming Language》

    简介 欢迎使用 Swift 关于 Swift 版本兼容性 Swift 初见 Swift 版本历史记录 Swift 教程 基础部分 基本运算符 字符串和字符 集合类型 控制流 函数 闭包 枚举 类和结构 ...

  2. Swift教程Swift语言快速入门(内部资料)

    Swift语言快速入门(内部资料) 试读下载地址:http://pan.baidu.com/s/1eQCGRHw 前言Swift教程Swift语言快速入门(内部资料)Swift教程Swift语言快速入 ...

  3. iOS 8 Metal Swift教程 :开始学习

    (via:泰然网,译者:Hero Kingsley) 学习使用苹果GPU加速3D绘图的新API:Metal! 在iOS 8里,苹果发布了一个新的接口叫做Metal,它是一个支持GPU加速3D绘图的AP ...

  4. 【note】the swift programming language Swift教程笔记

    // ------–Swift教程--------------- 要么变量初始化让其可以推断出类型,要么直接指定一个类型,不可以写: var a 这样的语句,会让系统不明白a到底是什么类型的变量 允许 ...

  5. 简明Python3教程 8.控制流

    简介 迄今为止我们见到的所有程序总是含有一连串语句并且python忠实的顺序执行它们. 那么如何改变它们的执行顺序呢?例如你希望程序根据不同情况作出不同反应,按照当前时间分别 打印出'Good Mor ...

  6. swift 简单语句 控制流语句

    2019独角兽企业重金招聘Python工程师标准>>> 在 Swift 中,有两种类型的语句:简单语句和控制流语句.简单语句是最常见的,用于构造表达式和声明.控制流语句则用于控制程序 ...

  7. Swift教程-视频拍摄教程

    原文链接:Take Video Tutorial in iOS8 with Swift 原文日期:2015/07/09 译者:lfb_CD 校对:numbbbbb 定稿:shanks 苹果公司提供了U ...

  8. 游戏中的三角学——Sprite Kit 和 Swift 教程(1)

    原文链接 : Trigonometry for Games – Sprite Kit and Swift Tutorial: Part 1/2 原文作者 : Nick Lockwood 译文出自 : ...

  9. HealthKit Swift 教程: workout

    原文:HealthKit Tutorial with Swift: Workouts 作者:Ted Bendixson 译者:kmyhy 更新说明:本教程由 Ted Bendixson 升级至 Swi ...

最新文章

  1. 最新!2020中国高校毕业生薪资报告出炉
  2. 解决oracle主键问题,解决renren-security使用oracle主键问题
  3. 《信息学奥赛一本通》 高精除以低精。输入两个正整数,求它们的商(做整除)。
  4. hihocoder 1343 : Stable Members【拓扑排序】
  5. java代码初体验_第一次Java 8体验
  6. 2018 年,NLP 研究与应用进展到什么水平了?
  7. String 属于基础的数据类型吗?
  8. hdu 1213 HowManyTables 并查集
  9. jquery 逗号分割截取字符串_JS/JQUERY字符串截取分割匹配等处理汇总
  10. python语言的开发者_写给.NET开发者的Python教程(一):引言
  11. python 中re模块学习随笔
  12. 系统更新链接服务器超时,win10系统更新导致Dr.com连接认证服务器超时的解决方法...
  13. modbus tcp主站和从站_实例讲解PLC实现modbus通讯
  14. 机器学习算法概述——摘录《机器学习算法导论》
  15. mybatis 使用简单的增删查改
  16. 安卓开发者模式中将最小dp调得太高导致无法开机的解决方法
  17. 冬瓜哥对时间和空间的理解方式—时空参悟(上)
  18. 六年一轮回:大数据改变的,不仅仅是我的专业!
  19. Messari:21年第二季度Web3及NFT报告
  20. 实验:4级流水线32bits全加器

热门文章

  1. 打破“维度的诅咒”,机器学习降维大法好
  2. 看!那全球最伟大的人工智能女性科学家们!
  3. 容量是GPT-2的1.7倍!谷歌打造神经对话模型Meena
  4. 基于python的人工智能Jiagu深度学习自然语言处理开源工具
  5. SAP QM 采购货物收到第三方仓库,转库回工厂仓库之后质检之处理
  6. 打破 Google 破坏性的搜索垄断
  7. 我们为什么要探索人脑的奥秘?
  8. 生物战教训、生物安全问题以及未来监控军民两用生物技术扩散的手段
  9. 《工业大数据白皮书》2019版正式发布
  10. 自动驾驶技术如何升级?这份技能图谱为你指路