Swift 流程控制
2016-07-27 18:00
399 查看
一.for循环
for-in
实例1:
如果括号内不使用index,可以用_代替:
遍历数组:
遍历字典:
二。while循环
语法:
原来的do...while循环在此处替换成了repeat...while循环。(不举例了)
三。switch
实例:
* 注意:与C和Objective_C不同,3.0的swift中switch语句的case中不再需要使用break跳出switch语句,匹配条件后即跳出Switch语句,同时,switch中的default是必须的。
虽然break在switch中不再是必须的,但是由于switch中的case/default中语句不允许有空语句,此时,我们可以使用break跳出语句。
例如:
再看下面这个例子
间隔匹配:
例如:
值绑定:
where:Switch中还可以使用where来设置附加条件
例如:
控制转移语句:
1.continue;
2.break(不再重复说明);
3.fallthrough;
4.return.
5.throw。
continue:跳出当前循环,执行下一次循环。
例如:
fallthrough:条件匹配成功后,继续执行后面的语句。
例如:
return:跳出整个循环与语句。
for-in
实例1:
for index in 1...5{ print("\(index)times 5 is \(index * 5)") }结果:
1times 5 is 5 2times 5 is 10 3times 5 is 15 4times 5 is 20 5times 5 is 251...5表示:[1,5]大于等于1且小于等于5
如果括号内不使用index,可以用_代替:
var a = 5 for _ in 1...5{ a += 1 } print("\(a)")结果:
10
遍历数组:
let nameArr = ["Lili","Marry","xiaoming"] for name in nameArr { print("Hello,\(name)!") }结果:
Hello,Lili! Hello,Marry! Hello,xiaoming!
遍历字典:
let ageDict = ["Lili":9,"Marry":10,"xiaoming":12] for (name,age) in ageDict { print("\(name) is \(age)!") }结果:
Marry is 10! Lili is 9! xiaoming is 12!由于字典是无序的,所以结果不一定按顺序输出。
二。while循环
语法:
while condition { statements }举例:
//唉,举例无能 var a = 10 while a>0 { a = a-2 } print(a)结果:0
原来的do...while循环在此处替换成了repeat...while循环。(不举例了)
三。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") }结果:
The first letter of the alphabet
* 注意:与C和Objective_C不同,3.0的swift中switch语句的case中不再需要使用break跳出switch语句,匹配条件后即跳出Switch语句,同时,switch中的default是必须的。
虽然break在switch中不再是必须的,但是由于switch中的case/default中语句不允许有空语句,此时,我们可以使用break跳出语句。
例如:
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") } if let integerValue = possibleIntegerValue { print("The integer value of \(numberSymbol) is \(integerValue).") } else { print("An integer value could not be found for \(numberSymbol).") }case "a":是无效的,这将导致程序报错: 'case' label in a 'switch' should have at least one executable statement。
再看下面这个例子
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."此时,default中的可能性很多,我们可能不再需要设置一个possibleIntegerValue的值,此时就可以使用break跳出switch语句。
间隔匹配:
例如:
let grade = 67 switch grade { case 0...59: print("不及格") case 60...79: print("及格") case 80...89: print("良好") case 90...100: print("优秀") default: break } //输出:及格元祖(Tuples)匹配:
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") } // 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"
where:Switch中还可以使用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") } // Prints "(1, -1) is on the line x == -y"
控制转移语句:
1.continue;
2.break(不再重复说明);
3.fallthrough;
4.return.
5.throw。
continue:跳出当前循环,执行下一次循环。
例如:
let puzzleInput = "great minds think alike" var puzzleOutput = "" for character in puzzleInput.characters { switch character { case "a", "e", "i", "o", "u", " ": continue default: puzzleOutput.append(character) } } print(puzzleOutput) // Prints "grtmndsthnklk"
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."
return:跳出整个循环与语句。
相关文章推荐
- swift - UIDatePicker 的用法
- iOS后台下载图片并实现本地通知(Swift)
- swift - 各种手势用法大全
- 03-swift微博搭建框架
- 02-swift微博自定义log
- IOS(swift语言)第3天、类,继承
- Swift基础(三十一)UINavigationController
- Swift之沙盒与数据存储
- IOS学习笔记-获取当前的地理位置swift版
- iOS NSKeyedArchiver序列化后进行数据持久化(swift)
- Swift基础(三十)UISearchBar
- swift学习第一天
- swift实现导航栏上展示城市名称,城市名称过长则隐藏部分
- Swift基础(二十八)UIPickerView
- Swift 3.0 Preview 1更新内容中文翻译
- 通过storyboard的exit返回到上页
- 六.swift - 函数
- 五.swift - 数组和字典
- 四.swift - string
- 二.swift的基本语法