在跟著書(書名很長,碁峯出版,作者柯博文)一面試基本的操作語法,發現有些跟書不一樣了,可能是swift本身有在更新,像for迴圈:
書上寫:
for var index = 0 ; index < 3; index++ {
print("index is \(index)")
}
執行結果會是
index = 0
index = 1
index = 2
但即時編譯時跑出訊息說以後不再支援這種像C的寫法,
會改成:
for index in 0 ..< 3 {
print("index is \(index)")
}
(注意第一行0 ..< 3空格的地方要對)
精省也直覺了不少,而且不用像VB要dim(宣告)東西一樣先用var定義。
下面講另一種範圍的loop,
書上寫:
for index in range(3):
print("index = \(index)")
現在也行不通了,要改成:
for index in 4 ... 6 {
print("index = \(index)")
}
結果就會是:
index = 4
index = 5
index = 6
沒有留言:
張貼留言