空合运算符(Nil Coalescing Operator) a ?? b ---> a != nil ? a! : b a必须是optional的:
let defaultColorName = "red" var userDefinedColorName: String? var colorNameToUse = userDefinedColorName ?? defaultColorName userDefinedColorName = "gree" colorNameToUse = userDefinedColorName ?? defaultColorName
区间运算符 ... ..<
for index in 1...5 { print(index) }
集合操作:
image = UIImage(named: "SetOperations") let oddDigits: Set = [1,3,5,7,9] let evenDigits: Set = [0,2,4,6,8] let singleDigitPrimeNumbers: Set = [2,7] // 并集 oddDigits.union(evenDigits).sort() // 交集 oddDigits.intersect(evenDigits).sort() // 补集 oddDigits.subtract(evenDigits).sort() oddDigits.exclusiveOr(singleDigitPrimeNumbers).sort() // 集合关系和比较 //使用“是否等”运算符(==)来判断两个集合是否包含相同的值。 //使用isSubsetOf(_:)方法来判断一个集合中的值是否也被包含在另外一个集合中。 //使用isSupersetOf(_:)方法来判断一个集合中包含的值是另一个集合中所有的值。 //使用isStrictSubsetOf(_:)或者isStrictSupersetOf(_:)方法来判断一个集合是否是另外一个集合的子集合或者父集合,并且和特定集合不相等。 //使用isDisjointWith(_:)方法来判断两个结合是否不含有相同的值。 image = UIImage(named: "setEulerDiagram") let houseAnimals: Set = ["1","2"] let farmAnimals: Set = ["1","2","3","4","5"] let cityAnimals: Set = ["6","7"] houseAnimals.isSubsetOf(farmAnimals) // true farmAnimals.isSupersetOf(houseAnimals) // true farmAnimals.isDisjointWith(cityAnimals) // true
Switch 与其他语言不同,不用在每个case中写break,swift只会执行一个case。switch语句必须是完备的。这就是说,每一个可能的值都必须至少有一个 case 分支与之对应。在某些不可能涵盖所有值的情况下,你可以使用默认(default)分支满足该要求,这个默认分支必须在switch语句的最后面。case中的条件可以是多个,并且可以是任意类型
let approximateCount = 62 let countedThings = "moons orbiting Saturn" var naturalCount: String switch approximateCount { case 0: naturalCount = "no" case 1..<5: naturalCount = "a few" case 5..<12: naturalCount = "several" case 12..<100: naturalCount = "dozens of" case 100..<1000: naturalCount = "hundreds of" default: naturalCount = "many" } print("There are \(naturalCount) \(countedThings).")Tuples 元组
let somePoint = (1,1) switch somePoint { case (0,0): print("(0,0) is at the origin") case (_,0): // x任意 y==0 print("(\(somePoint.0),0) is on the x-axis") case (0,_): // x==0 y任意 print("(0,\(somePoint.1)) is on the y-axis") case (-2...2,-2...2): print("(\(somePoint.0),\(somePoint.1)) is inside the Box") default: print("(\(somePoint.0),\(somePoint.1)) is outside the Box") } image = UIImage(named: "coordinateGraphSimple") // 虽然上面如果point为(0,0)的时候,会满足所有的case,但是swift只会选择第一个满足的执行
let anotherPoint = (2,0) // 注意下面是没有default的 // 下面的let声明,可以改成var声明 switch anotherPoint { case (let x,0): print("on the x-axis with an x value of \(x)") case (0,let y): print("on the y-axis whth a y value of \(y)") case let (x,y): print("somewhere else at (\(x),\(y))") } image = UIImage(named: "coordinateGraphMedium") // Where let yetAnotherPoint = (1,-1) switch yetAnotherPoint { case let (x,y) where x == y: print("(\(x),\(y)) is on the line x == y") case let (x,y) where x == -y: print("(\(x),\(y)) is on the line x == -y") case let (x,y): print("(\(x),\(y)) is just some arbitrary point") } image = UIImage(named: "coordinateGraphComplex")Fallthrough Swift 中的switch不会从上一个 case 分支落入到下一个 case 分支中。相反,只要第一个匹配到的 case 分支完成了它需要执行的语句,整个switch代码块完成了它的执行。你确实需要 C 风格的贯穿(fallthrough)的特性,你可以在每个需要该特性的 case 分支中使用fallthrough关键字
let integerToDescribe = 5 var description = "The number \(integerToDescribe) is" switch integerToDescribe { case 2,11,13,17,19: description += " a prime number,and also" fallthrough default: description += " an integer." } print(description)