1策肝、使用===
和!===
比較兩個對象是否相等
2侄榴、可選值與三目運(yùn)算配合使用減少代碼量:
var a: Int?
let b: Int = 2
let c = a ?? b
當(dāng) a 不為空時立砸,c=a, 當(dāng) a 為空時斩狱, c=b
等同于
let a:Int? = 2
let b:Int = 2
let c = (a != nil) ? a : b
3耳高、String在被傳遞給方法或者被賦值給其他變量或者常量的時候會被復(fù)制,然后將復(fù)制的那個string 傳遞過去喊废,而不是原來那個數(shù)值,不必?fù)?dān)心因為某個值改變了而影響初始值
4祝高、計算String長度:
let stringCount = someSTring.character.count
5、string下標(biāo):
var originString = "apple"
let startIdx = originString.startIndex // 0
let endIndex = originString.endIndex// 5,不是一個合法的下標(biāo)
let succeesor = startIdx.successor() //1
let predecessor = endIndex.predecessor() //4
originString[succeesor] //p
originString[predecessor] //e
//開始下標(biāo)往后兩個的字母
originString[startIdx.advancedBy(2)] //p
移除子字符串
let startRange = originString.startIndex
let endRange = originString.startIndex.advancedBy(2)
let range = startRange ..< endRange
originString.removeRange(range) //prints ""
6 污筷、使用下標(biāo)修改數(shù)組元素
使用下標(biāo)修改數(shù)組元素時工闺,可以同時修改一個范圍的元素,如瓣蛀,修改數(shù)組0~2的元素:
let arr = ["a","b","c","d","e","f"]
arr[0...2] = ["z","y","x"]
允許修改的元素個數(shù)與所給定的范圍不一致:
let arr = ["a","b","c","d","e","f"]
arr[0...1] = ["z","y","x"]
//這個時候 arr = ["z","y","x","c","d","e","f"],相當(dāng)于用z,y,x替代了a,b
7陆蟆、用key值訪問字典的value,將這個value設(shè)置為nil時惋增,這個key-value會從字典中移除:
var stringDic = ["a":1,"b":2,"c":3];
stringDic["a"] = nil
//現(xiàn)在 stringDic = ["b":2,"c":3]
8叠殷、單獨(dú)獲取字典的所有key值或者value值,并用數(shù)組表示:
var stringDic:[String:AnyObject] = ["a":1,"b":2,"c":3];
let keys = [String](stringDic.keys)
let values = [AnyObject](stringDic.values)
字典是無序的诈皿,所以得到的keys和values也是無序的林束,如果需要特定排序像棘,調(diào)用sort()
方法:
let keys = [String](stringDic.keys.sort())
9、使用fallthrough
使得switch中的case都往下執(zhí)行操作壶冒,不會自動跳出:
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."
}
// description = "The number 5 is a prime number, and also an integer."
如果再加一個case:
let integerToDescribe = 4
var description = "The number \(integerToDescribe) is"
// description = "The number 4 is a even number, and also a prime number, and also an integer."
switch integerToDescribe {
case 4,6,8:
description += " a even number, and also"
fallthrough
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also"
fallthrough
default:
description += " an integer."
}
10缕题、使用元組表示switch
的判斷條件:
用坐標(biāo)系為例,
let point:(Double,Double) = (1.0,1.0)
let description:String?
switch point {
case (0..2,0..2):
description = "第一象限"
case (-2..<0,0...2):
description = "第二象限"
case (-2..<0,-2..<0):
description = "第三象限"
case (0..2,-2..<0):
description = "第四象限"
default:
desc = "out of range"
break
}
11胖腾、switch
的值綁定(value binding)
switch
只判斷元組中的某一個判斷條件
let descp:String?
let p = (1,1)
// p = (1,0): descp = "on the x-axis with an value of 1."
// p = (1,1):descp = "p is at x: 1, y: 1"
switch p {
case (let x, 0):
descp = "on the x-axis with an value of \(x)."
case (0, let y):
descp = "on the x-axis with an value of \(y)."
case (0,0):
descp = "p is in origin point"
case (let x, let y):
descp = "p is at x: \(x), y \(y)"
}
12烟零、 switch
中的where
在switch
中用where
添加額外的判斷:
let descp:String?
let p = (1,1)
// p = (1,-1): descp = "x is equal to -y."
// p = (1,1): descp = "x is equal to y."
// p = (3,1): descp = "x is more than y."
switch p {
case (let x, let y) where x == y:
descp = "x is equal to y."
case (let x, let y) where x == -y:
descp = "x is equal to -y."
case (let x, let y) where x > y:
descp = "x is more than y."
13、 系統(tǒng)版本判斷
if #available(*platform name* *version*, *...*, *) {
//statements to execute if the APIs are available
} else {
//fallback statements to execute if the APIs are unavailable
}