1、使用===
和!===
比较两个对象是否相等
2、可选值与三目运算配合使用减少代码量:
var a: Int?
let b: Int = 2
let c = a ?? b
当 a 不为空时,c=a, 当 a 为空时, c=b
等同于
let a:Int? = 2
let b:Int = 2
let c = (a != nil) ? a : b
3、String在被传递给方法或者被赋值给其他变量或者常量的时候会被复制,然后将复制的那个string 传递过去,而不是原来那个数值,不必担心因为某个值改变了而影响初始值
4、计算String长度:
let stringCount = someSTring.character.count
5、string下标:
var originString = "apple"
let startIdx = originString.startIndex // 0
let endIndex = originString.endIndex// 5,不是一个合法的下标
let succeesor = startIdx.successor() //1
let predecessor = endIndex.predecessor() //4
originString[succeesor] //p
originString[predecessor] //e
//开始下标往后两个的字母
originString[startIdx.advancedBy(2)] //p
移除子字符串
let startRange = originString.startIndex
let endRange = originString.startIndex.advancedBy(2)
let range = startRange ..< endRange
originString.removeRange(range) //prints ""
6 、使用下标修改数组元素
使用下标修改数组元素时,可以同时修改一个范围的元素,如,修改数组0~2的元素:
let arr = ["a","b","c","d","e","f"]
arr[0...2] = ["z","y","x"]
允许修改的元素个数与所给定的范围不一致:
let arr = ["a","b","c","d","e","f"]
arr[0...1] = ["z","y","x"]
//这个时候 arr = ["z","y","x","c","d","e","f"],相当于用z,y,x替代了a,b
7、用key值访问字典的value,将这个value设置为nil时,这个key-value会从字典中移除:
var stringDic = ["a":1,"b":2,"c":3];
stringDic["a"] = nil
//现在 stringDic = ["b":2,"c":3]
8、单独获取字典的所有key值或者value值,并用数组表示:
var stringDic:[String:AnyObject] = ["a":1,"b":2,"c":3];
let keys = [String](stringDic.keys)
let values = [AnyObject](stringDic.values)
字典是无序的,所以得到的keys和values也是无序的,如果需要特定排序,调用sort()
方法:
let keys = [String](stringDic.keys.sort())
9、使用fallthrough
使得switch中的case都往下执行操作,不会自动跳出:
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
的判断条件:
用坐标系为例,
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、 系统版本判断
if #available(*platform name* *version*, *...*, *) {
//statements to execute if the APIs are available
} else {
//fallback statements to execute if the APIs are unavailable
}
网友评论