Swift学习笔记。

前端之家收集整理的这篇文章主要介绍了Swift学习笔记。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

空合运算符(Nil Coalescing Operator) a ?? b ---> a != nil ? a! : b a必须是optional的:

  1. let defaultColorName = "red"
  2. var userDefinedColorName: String?
  3. var colorNameToUse = userDefinedColorName ?? defaultColorName
  4.  
  5. userDefinedColorName = "gree"
  6. colorNameToUse = userDefinedColorName ?? defaultColorName

区间运算符 ... ..<
  1. for index in 1...5 {
  2. print(index)
  3. }

集合操作:
  1. image = UIImage(named: "SetOperations")
  2.  
  3.  
  4. let oddDigits: Set = [1,3,5,7,9]
  5. let evenDigits: Set = [0,2,4,6,8]
  6. let singleDigitPrimeNumbers: Set = [2,7]
  7. // 并集
  8. oddDigits.union(evenDigits).sort()
  9. // 交集
  10. oddDigits.intersect(evenDigits).sort()
  11. // 补集
  12. oddDigits.subtract(evenDigits).sort()
  13.  
  14.  
  15. oddDigits.exclusiveOr(singleDigitPrimeNumbers).sort()
  16.  
  17.  
  18. // 集合关系和比较
  19. //使用“是否等”运算符(==)来判断两个集合是否包含相同的值。
  20. //使用isSubsetOf(_:)方法来判断一个集合中的值是否也被包含在另外一个集合中。
  21. //使用isSupersetOf(_:)方法来判断一个集合中包含的值是另一个集合中所有的值。
  22. //使用isStrictSubsetOf(_:)或者isStrictSupersetOf(_:)方法来判断一个集合是否是另外一个集合的子集合或者父集合,并且和特定集合不相等。
  23. //使用isDisjointWith(_:)方法来判断两个结合是否不含有相同的值。
  24.  
  25.  
  26. image = UIImage(named: "setEulerDiagram")
  27.  
  28.  
  29. let houseAnimals: Set = ["1","2"]
  30. let farmAnimals: Set = ["1","2","3","4","5"]
  31. let cityAnimals: Set = ["6","7"]
  32. houseAnimals.isSubsetOf(farmAnimals)
  33. // true
  34. farmAnimals.isSupersetOf(houseAnimals)
  35. // true
  36. farmAnimals.isDisjointWith(cityAnimals)
  37. // true


Switch 与其他语言不同,不用在每个case中写break,swift只会执行一个case。switch语句必须是完备的。这就是说,每一个可能的值都必须至少有一个 case 分支与之对应。在某些不可能涵盖所有值的情况下,你可以使用默认(default)分支满足该要求,这个默认分支必须在switch语句的最后面。case中的条件可以是多个,并且可以是任意类型

  1. let approximateCount = 62
  2. let countedThings = "moons orbiting Saturn"
  3. var naturalCount: String
  4. switch approximateCount {
  5. case 0:
  6. naturalCount = "no"
  7. case 1..<5:
  8. naturalCount = "a few"
  9. case 5..<12:
  10. naturalCount = "several"
  11. case 12..<100:
  12. naturalCount = "dozens of"
  13. case 100..<1000:
  14. naturalCount = "hundreds of"
  15. default:
  16. naturalCount = "many"
  17. }
  18. print("There are \(naturalCount) \(countedThings).")
Tuples 元组
  1. let somePoint = (1,1)
  2. switch somePoint {
  3. case (0,0):
  4. print("(0,0) is at the origin")
  5. case (_,0): // x任意 y==0
  6. print("(\(somePoint.0),0) is on the x-axis")
  7. case (0,_): // x==0 y任意
  8. print("(0,\(somePoint.1)) is on the y-axis")
  9. case (-2...2,-2...2):
  10. print("(\(somePoint.0),\(somePoint.1)) is inside the Box")
  11. default:
  12. print("(\(somePoint.0),\(somePoint.1)) is outside the Box")
  13. }
  14. image = UIImage(named: "coordinateGraphSimple")
  15. // 虽然上面如果point为(0,0)的时候,会满足所有的case,但是swift只会选择第一个满足的执行

  1. let anotherPoint = (2,0)
  2. // 注意下面是没有default的
  3. // 下面的let声明,可以改成var声明
  4. switch anotherPoint {
  5. case (let x,0):
  6. print("on the x-axis with an x value of \(x)")
  7. case (0,let y):
  8. print("on the y-axis whth a y value of \(y)")
  9. case let (x,y):
  10. print("somewhere else at (\(x),\(y))")
  11. }
  12. image = UIImage(named: "coordinateGraphMedium")
  13.  
  14. // Where
  15. let yetAnotherPoint = (1,-1)
  16. switch yetAnotherPoint {
  17. case let (x,y) where x == y:
  18. print("(\(x),\(y)) is on the line x == y")
  19. case let (x,y) where x == -y:
  20. print("(\(x),\(y)) is on the line x == -y")
  21. case let (x,y):
  22. print("(\(x),\(y)) is just some arbitrary point")
  23. }
  24. image = UIImage(named: "coordinateGraphComplex")
Fallthrough Swift 中的switch不会从上一个 case 分支落入到下一个 case 分支中。相反,只要第一个匹配到的 case 分支完成了它需要执行的语句,整个switch代码块完成了它的执行。你确实需要 C 风格的贯穿(fallthrough)的特性,你可以在每个需要该特性的 case 分支中使用fallthrough关键字
  1. let integerToDescribe = 5
  2. var description = "The number \(integerToDescribe) is"
  3. switch integerToDescribe {
  4. case 2,11,13,17,19:
  5. description += " a prime number,and also"
  6. fallthrough
  7. default:
  8. description += " an integer."
  9. }
  10. print(description)

猜你在找的Swift相关文章