常量和變量
常量和變量把一個(gè)名字(比如maximumNumberOfLoginAttempts或者welcomeMessage)和一個(gè)指定類(lèi)型的
值(比如數(shù)字10或者字符串"Hello")關(guān)聯(lián)起來(lái)。常量的值一旦設(shè)定就不能改變,而變量的值可以隨意更
1.聲明一個(gè) 常量?
let a = "Hello World"
如果想明確這個(gè)常量的類(lèi)型
( 在swift 里每句結(jié)束后不是必須使用“;” 分號(hào) ,? 有一種情況下必須要用分號(hào),即你打算在同一行內(nèi)寫(xiě)多條獨(dú)立的語(yǔ)句 )?
let? a : string =? "Hello World"? ? ? ? ;? ? let myVariable ?:float_t ?= ?42
1.聲明一個(gè) 變量
var a = "Hello World"
a = "Haha"
打印信息
print( a ) ? ?// ?print( \( a ) )
//? ? ? ? 初始化空字符串
let stringA = ""
let stringB = String()
if stringA.isEmpty && stringB.isEmpty{
print("他倆是空的,Nothing to see here")
}
//字符串的拼接 ? ?直接用" + "
let a = "The width is "
let b = 94
let widthLabel = a + String(b) // 轉(zhuǎn)換成string 類(lèi)型
print(widthLabel)
創(chuàng)建一個(gè)空數(shù)組
var emptyArray = [String]()
向數(shù)組中插入一個(gè)數(shù)據(jù) ? append
emptyArray.append(a)
print(emptyArray)
//遍歷數(shù)組 ? 和判斷 ?
遍歷一般用 ?for in ? ? ?判斷則不用if(){} ? 直接 用if ?{} ?
let value = [22,11,77,33,44,55]
for scoree in value{
if scoree > 30{
print("aaaaaaa")
}else{
print("bbbbbbb")
}
if scoree > 20 {
let name = "Hello World"
print(name)
}
}
//兩個(gè)數(shù)組相加
let oneArray = ["1","2","3"]
let? twoArray? = ["4","5","6"]
var threeArray = oneArray + twoArray
//? 遍歷數(shù)組? 然后根據(jù)下表刪除
for (index,value) in threeArray.enumerated() {
if value == "3" {
threeArray.remove(at: index)
}
}
print(threeArray)
Tuples 元組
元組(tuples)把多個(gè)值組合成一個(gè)復(fù)合值。元組內(nèi)的值可以是任意類(lèi)型,并不要求是相同類(lèi)型。
let http404Error = (404, "Not Found")
// http404Error 的類(lèi)型是 (Int, String),值是 (404, "Not Found")
你可以把任意順序的類(lèi)型組合成一個(gè)元組,這個(gè)元組可以包含所有類(lèi)型。只要你想,你可以創(chuàng)建一個(gè)類(lèi)型為( Int , Int ,Int)或者 (string, Bool) 或者其他任何你想要的組合的元組。
你可以將一個(gè)元組的內(nèi)容分解(decompose)成單獨(dú)的常量和變量,然后你就可以正常使用它們了:
let (statusCode, statusMessage) = http404Errorprint("The status code is \(statusCode)")
// 輸出 "The status code is 404"
print("The status message is \(statusMessage)")// 輸出 "The status message is Not Found"
如果你只需要一部分元組值,分解的時(shí)候可以把要忽略的部分用下劃線(xiàn)(_)標(biāo)記:
let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
// 輸出 "The status code is 404"
此外,你還可以通過(guò)下標(biāo)來(lái)訪(fǎng)問(wèn)元組中的單個(gè)元素,下標(biāo)從零開(kāi)始:
print("The status code is \(http404Error.0)")
// 輸出 "The status code is 404"
print("The status message is \(http404Error.1)")// 輸出 "The status message is Not Found"
你可以在定義元組的時(shí)候給單個(gè)元素命名:
let http200Status = (statusCode: 200, description: "OK")
給元組中的元素命名后,你可以通過(guò)名字來(lái)獲取這些元素的值:
print("The status code is \(http200Status.statusCode)")
// 輸出 "The status code is 200"
print("The status message is \(http200Status.description)")// 輸出 "The status message is OK"
作為函數(shù)返回值時(shí),元組非常有用。一個(gè)用來(lái)獲取網(wǎng)頁(yè)的函數(shù)可能會(huì)返回一個(gè)(Int, String)元組來(lái)描述是否獲取成功。和只能返回一個(gè)類(lèi)型的值比較起來(lái),一個(gè)包含兩個(gè)不同類(lèi)型值的元組可以讓函數(shù)的返回信息更有用
可選類(lèi)型
基本就是 返回值的時(shí)候如果不確定 是 不是空 或者返回來(lái)的是不是 一個(gè)確定的類(lèi)型 就用一個(gè)??
var surveyAnswer: String? ? ? ? // surveyAnswer 被自動(dòng)設(shè)置為 nil
if? 判斷語(yǔ)句 基本語(yǔ)法跟Objective-C ?一樣 ? 在|| 的語(yǔ)句中 ?如果想強(qiáng)調(diào)某個(gè)條件是首先判斷的話(huà) ? ?
let a = true
let b = true
let c = true
if a || b || (c){
print("I don't want? say something。。。")
}