Swift讲解专题六——流程控制
一、引言
一种编程语言的强大与否,很大程度上取决于其提供的程序流程控制方案,就如使用汇编语言实现复杂的程序流程是一件痛苦的事情。Swift中提供了许多强大的流程控制语句,例如快速遍历for-in,while循环,repeat-while循环,switch选择等,需要注意的是,在Swift2.2中,for(a;b;c)循环已经被弃用掉,并且Swift中的Switch语句也更加强大,可以处理任意数据类型。
二、for-in循环
配合范围运算符,for-in循环可以用来执行确定次数的循环,示例如下:
1for index in 1...5 { 2 print(index) 3} 4//如果不需要获取循环中每次的循环次数 可以使用如下方式 5var sum=0; 6for _ in 1...3 { 7 sum += 1 8}
for-in循环也通常会用来遍历数组,字典,集合等,示例如下:
1var collection1:Array = [1,2,3,4] 2var collection2:Dictionary = [1:1,2:2,3:4,4:4] 3var collection3:Set = [1,2,3,4] 4for obj in collection1 { 5 print(obj) 6} 7for (key , value) in collection2 { 8 print(key,value) 9} 10for obj in collection3 { 11 print(obj) 12}
三、while循环
while语句进行循环操作,直到循环条件为false为止,这类型的循环通常适用于循环次数不定的循环需求,while循环提供两种语法格式,示例如下:
1var i=0 2//当i不小于10时跳出循环 3while i<10 { 4 print("while",i) 5 i+=1 6} 7//先进行一次操作 在判断循环条件 8repeat { 9 print("repeat while") 10} while i<10
四、if语句
if语句是程序开发中最常用的语句之一,通过if将判断一个条件是否成立来进行程序的流程控制,if语句通常会和else语句结合进行使用,示例如下:
1var c:Int 2if 1>2 { 3 c=1 4}else if 1<0 { 5 c=2 6}else{ 7 c=3 8}
五、Switch语句
Switch语句作为开关选择语句,用来处理一组值的分支选择,Swift中的Switch语句格外强大,相比于Objective-C,Swift中的Switch语句每个case后不需要使用break进行手动中断,当代码匹配到一个case后语句将自行中断。用法示例代码如下:
1var charac:Character = "b" 2//使用switch语句进行字符分支判断 3switch charac { 4case "a": 5 print("chara is a") 6case "b": 7 print("chara is b") 8case "c": 9 print("chara is c") 10default ://default用于处理其他额外情况 11 print("no charac") 12} 13//同一个case中可以包含多个分支 14switch charac { 15case "a","b","c" : 16 print("chara is word") 17case "1","2","3" : 18 print("chara is num") 19default : 20 print("no charac") 21} 22//在case中也可以使用一个范围 23var num = 3 24switch num { 25case 1...3 : 26 print("1<=num<=3") 27case 4 : 28 print("chara is num") 29default : 30 print("no charac") 31} 32//使用Switch语句进行元组的匹配 33var tuple = (0,0) 34switch tuple { 35case (0,1): 36 print("Sure") 37 //也可以只对元组中的某个元素进行匹配 38case (_,1): 39 print("Sim") 40 //也可以对元组中的元素进行范围匹配 41case(0...3,0...3): 42 print("SIM") 43default: 44 print("") 45} 46//进行数据绑定 47switch tuple { 48case (let a,1): 49 print(a) 50case (let b,0): 51 print(b) 52 //let(a,b) 与 (let a,let b)意义相同 53case let(a,b): 54 print(a,b) 55default: 56 print("") 57} 58//对于进行了数据绑定的Switch语句 可以使用where关键字来进行条件判断 59switch tuple { 60case (let a,1): 61 print(a) 62case (let b,0): 63 print(b) 64//let(a,b) 与 (let a,let b)意义相同 65case let(a,b) where a==b: 66 print(a,b) 67default: 68 print("") 69}
六、跳转语句
Swift中提供了5种跳转语句,continue,break,fallthrough,return,throw。
continue:跳出到循环起始位置,直接开始下次循环。
break:break如果在循环语句中则是直接中断循环,跳出,若是在Switch结构中,则立即跳出Switch结构。
fallthrough语句需要和switch语句配合使用,在case中使用fallthrough,则会继续执行下一个case,需要注意,在下一个case中有进行数据绑定的,不可以使用fallthrough,示例如下:
1var tuple = (0,0) 2switch tuple { 3case (0,0): 4 print("Sure") 5 //fallthrough会继续执行下面的case 6 fallthrough 7 //也可以只对元组中的某个元素进行匹配 8case (_,0): 9 print("Sim") 10 fallthrough 11 //也可以对元组中的元素进行范围匹配 12case(0...3,0...3): 13 print("SIM") 14default: 15 print("") 16}
return:return语句直接从函数中返回。
throw:throw用于抛出异常。
Swift还支持另一种语法,可以为while循环设置一个tip标签,使用break和continue等关键字来进行流程的控制,示例如下:
1var tmp = 0; 2tip:while tmp<10 { 3 print("ccc") 4 tmp+=1 5 switch tmp { 6 case 3: 7 break tip 8 default: 9 break 10 } 11}
Swift2.0之后,提供了一种新的语法,guard-else,这也被称作守护语句,只有当条件不满足时,才执行else后面的代码,示例如下:
1var name = "HS" 2func nameChange(name:String) { 3 guard name=="HS" else{ 4 print(name) 5 return 6 } 7 print("name is HS") 8} 9 10nameChange(name)
在开发中,函数中常常会需要检查传入的参数是否符合标准,guard-else语句就是为这种需求所生,正如其名,它用于守护函数执行的精确度。
七、系统版本检查
使用如下示例代码进行系统支持版本的检查:
1if #available(iOS 9, *){ 2 print("iOS 9") 3}
专注技术,热爱生活,交流技术,也做朋友。
——珲少 QQ群:203317592