Swift学习笔记。

Swift学习笔记。,第1张

概述空合运算符(Nil Coalescing Operator) a ?? b ---> a != nil ? a! : b a必须是optional的: let defaultColorName = "red"var userDefinedColorName: String?var colorNameToUse = userDefinedColorName ?? defaultColorName

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

let defaultcolorname = "red"var userdefinedcolorname: String?var colornameToUse = userdefinedcolorname ?? defaultcolornameuserdefinedcolorname = "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)// truefarmAnimals.isSupersetof(houseAnimals)// truefarmAnimals.isdisjointWith(cityAnimals)// true


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

let approximateCount = 62let countedThings = "moons orbiting Saturn"var naturalCount: Stringswitch 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")// Wherelet 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 = 5var description = "The number \(integerToDescribe) is"switch integerToDescribe {case 2,11,13,17,19:    description += " a prime number,and also"    fallthroughdefault:    description += " an integer."}print(description)
总结

以上是内存溢出为你收集整理的Swift学习笔记。全部内容,希望文章能够帮你解决Swift学习笔记。所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1085811.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存