Swift和Java控制流比较
1. Swift中for in循环和Java中foreach很类似,都可以简化对集合的遍历操作。
Swift语法:
for index in 1...5{ print("the index is \(index)") } let base = 3 let power = 3 var answer = 1 for _ in 1...power { answer *= base } print("\(base) to the power of \(power) is \(answer)") // 输出 "3 to the power of 10 is 59049"
Java语法如下:
ArrayList<String> array = new ArrayList<String>(); array.add("a"); array.add("b"); array.add("c"); array.add("d"); for(String item: array){ System.out.println(item); }
2. for条件递增
Siwft和Java语法很类似,只是Swift语法没有括号而已
Swift语法如下:
for var index = 0; index < 3; ++index{ print("index = \(index)") }
注意index
在循环结束后最终的值是3
而不是2
。最后一次调用递增表达式++index
会将index
设置为3
,从而导致index < 3
条件为false
,并终止循环。
Java语法如下:
for(int index = 0; index < 3; index++){ System.out.println("index = " + index); }
3. Switch....case语法
Swift和Java语法差不多,Swift不用写break关键字,但是Swift支持更多case比较,下面是Swift代码:
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") }
在 Swift 中,当匹配的 case 分支中的代码执行完毕后,程序会终止switch
语句,而不会继续执行下一个 case 分支。这也就是说,不需要在 case 分支中显式地使用break
语句。这使得switch
语句更安全、更易用,也避免了因忘记写break
语句而产生的错误。每一个 case 分支都必须包含至少一条语句。像下面这样书写代码是无效的,因为第一个 case 分支是空的。
Java代码如下:
char someCharacter = 'e'; switch (someCharacter){ case 'e': System.out.println(""); break; }
Swift支持区间匹配
代码如下:
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).") // 输出 "There are millions and millions of stars in the Milky Way."
Swift支持值绑定
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))") } // 输出 "on the x-axis with an x value of 2"
case 分支的模式允许将匹配的值绑定到一个临时的常量或变量,这些常量或变量在该 case 分支里就可以被引用了——这种行为被称为值绑定(value binding)。
Swift支持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") } // 输出 "(1, -1) is on the line x == -y"
在上面的例子中,switch
语句会判断某个点是否在绿色的对角线x == y
上,是否在紫色的对角线x == -y
上,或者不在对角线上。
4. 控制转移语句(Control Transfer Statements)
Swift:控制转移语句改变你代码的执行顺序,通过它你可以实现代码的跳转。Swift有四种控制转移语句,关键字如下:continue、break、fallthrough、return。Java控制语句关键字少一个fallthrough,其他关键字用法接近。关于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) // 输出 "The number 5 is a prime number, and also an integer."
如果integerToDescribe
的值不属于列表中的任何质数,那么它不会匹配到第一个switch
分支。而这里没有其他特别的分支情况,所以integerToDescribe
匹配到包含所有的default
分支中。
注意:fallthrough
关键字不会检查它下一个将会落入执行的 case 中的匹配条件。fallthrough
简单地使代码执行继续连接到下一个 case 中的执行代码,这和 C 语言标准中的switch
语句特性是一样的。