標籤:swift
常量、變數
聲明常量:let
聲明變數:var
eg. let conNum = 1; var num = 2;
基礎資料型別 (Elementary Data Type)
Int、 Float、Double、Bool、tuples、optional
其中
tuples 元祖,可以包含不同類型值的集合;
optional 可選值,表示一個值可以為空白(nil),如果一個變數沒有聲明為optional,那麼必須給它一個確切的值;
類型轉換
需要指定轉換類型:Sometype(val)
運算子
與其他語言有相似的規則
特殊規則
- 可以對Float求餘,8 % 2.5 // 等於 0.5;
- 空值運算子 a ?? b,對可選類型a進行空判斷,如果a包含一個值就進行解鎖,否則就返回一個預設值b,運算式a必須是Optional類型,預設值b的類型必須要和a儲存值的類型保持一致
- 區間運算子
閉區間運算子(a...b); 半開區間(a..<b
字串
Swift 的String類型是實值型別。 如果您建立了一個新的字串,那麼當其進行常量、變數賦值操作或在函數/方法中傳遞時,會進行值拷貝。 任何情況下,都會對已有字串值建立新副本,並對該新副本進行傳遞或賦值操作。
數組
var shoppingList: [String] = ["Eggs", "Milk"]shoppingList.append("Flour")shoppingList += ["Chocolate Spread","Cheese","Butter"]shoppingList[0] = "Six eggs"shoppingList.insert("Maple Syrup", atIndex: 0)let mapleSyrup = shoppingList.removeAtIndex(0)for item in shoppingList { println(item)}
- 字典
var airports: [String:String] = ["TYO": "Tokyo", "DUB": "Dublin"]for (airportCode, airportName) in airports { println("\(airportCode): \(airportName)")}for airportCode in airports.keys { println("Airport code: \(airportCode)")}// Airport code: TYO// Airport code: LHRfor airportName in airports.values { println("Airport name: \(airportName)")}// Airport name: Tokyo// Airport name: London Heathrowlet airportCodes = Array(airports.keys)// airportCodes is ["TYO", "LHR"]let airportNames = Array(airports.values)// airportNames is ["Tokyo", "London Heathrow"]
數組和字典都是在單個集合中儲存可變值。如果我們建立一個數組或者字典並且把它分配成一個變數,這個集合將會是可變的。這意味著我們可以在建立之後添加更多或移除已存在的資料項目來改變這個集合的大小。與此相反,如果我們把數組或字典分配成常量,那麼它就是不可變的,它的大小不能被改變。
- for 迴圈
let names = ["Anna", "Alex", "Brian", "Jack"]for name in names { println("Hello, \(name)!")}let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]for (animalName, legCount) in numberOfLegs { println("\(animalName)s have \(legCount) legs")}for var index = 0; index < 3; ++index { println("index is \(index)")}
- while、do while
while condition { statements}do { statements} while condition
- if
var temperatureInFahrenheit = 90if temperatureInFahrenheit <= 32 { println("It‘s very cold. Consider wearing a scarf.")} else if temperatureInFahrenheit >= 86 { println("It‘s really warm. Don‘t forget to wear sunscreen.")} else { println("It‘s not that cold. Wear a t-shirt.")}
- switch
let someCharacter: Character = "e"switch someCharacter {case "a", "e", "i", "o", "u": println("\(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": println("\(someCharacter) is a consonant")default: println("\(someCharacter) is not a vowel or a consonant")}
這裡default語句是必須的,否則編譯不通過;不需要break,因為只會匹配一種情況。
case 中還可以加條件判斷where
let yetAnotherPoint = (1, -1)switch yetAnotherPoint {case let (x, y) where x == y: println("(\(x), \(y)) is on the line x == y")case let (x, y) where x == -y: println("(\(x), \(y)) is on the line x == -y")case let (x, y): println("(\(x), \(y)) is just some arbitrary point")}// 輸出 "(1, -1) is on the line x == -y"
- continue、break、fallthrough
continue、break與其他語言用法相同;
fallthrough用在switch語句中,實現條件貫穿,相當於Java或者其他語言case塊中沒有寫break的情況:
let integerToDescribe = 5var description = "The number \(integerToDescribe) is"switch integerToDescribe {case 2, 3, 5, 7, 11, 13, 17, 19: description += " a prime number, and also" fallthroughdefault: description += " an integer."}println(description)// 輸出 "The number 5 is a prime number, and also an integer."
Swift入門(一)