在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
fallthrough 贯穿 case 可以不必写break 如果不需要知道区间内 每一项的值 可以使用 下划线 —— 来代替变量名 忽略 对该值的访问 for index in 1...5{ println(index) } 求3的10次幂 let base = 3 let power = 10 var answer = 1 for _ in 1...power{ answer *=base }
let dog:Character = "????" let cow:Character = "????" let dogCow = dog + cow println(dogCow)
// swift 的算术运算符 不允许溢出,通过溢出运算符来选择值的溢出情况(a& + b) // 余数运算符 可以用于 浮点数 flost 8 % 2.5 // equals 0.5 // 一元减运算符 let three = 3 let minusTree = -three let plusThree = -minusTree println(minusTree) println(plusThree) // 一元加运算符
// let names = ["anna","alex","bbsc","sdfs"] // let count = names.count // for i in 0...count-1{ // println("person \(i) is called \(names[i])") // // } // let a = 5 // var b = 10 // b = a // if a = b{ // swift 中赋值运算符,并不将自身作为一个值进行返回,所以编译不合法,帮开发者避免错误,很人性化的语言 // }
// swift 中的switch 可以允许 case 多个 多号分开即可,且不用写break,多个匹配的会选择第一个,第二个并不进入,也可以是一个范围 let charA:Character = "a" switch charA{ case "a","A": println("haha") case "a": println("a") default: println("default") }
let count = 3_000_000_000 let countedThings = "stars in the Milky Way" var naturalCount:String switch count{ case 0: naturalCount = "no" case 1...3: naturalCount = "a few" case 10...99: naturalCount = "several" case 100...999: naturalCount = "hundreds of" case 1000...999_999: naturalCount = "thousands of" default: naturalCount = "millions and millions of" } println("there are \(naturalCount)\(countedThings)")
// 更实用 // 使用元组在同一个switch语句中测试多个值,元素可以是值,也可以是范围,使用下划线——来匹配所有可能的值 // 使用(Int,Int)类型的元组来分类下图中的点(x,y) let somePoint = (1,1) switch somePoint{ case (0,0): println("(0,0) is at the origin") case (_,0): println("(\(somePoint.0),0) is on the x-axis") case(0,_): println("(0,\(somePoint.1)) is on the y-axis") case(-2...2,-2...2): println("(\(somePoint.0),\(somePoint.1)) is inside the box") default: println("(\(somePoint.0),\(somePoint.1)) is outside the box") }
// 值绑定 vaLue bindings // case分值允许将匹配值绑定到一个临时变量或常量,在case 分支里可以引用 let anotherPoint = (2,0) switch anotherPoint{ case (let x,0): // x只是占位符,临时获取条件中一个或多个值 println("x value is \(x)") case (0,let y): println("y value is \(y)") case let (x,y): println("is (\(x),\(y))") } // 判断额外条件 let yetAnotherPoint = (1,-1) switch yetAnotherPoint { case let(x,y) where x == y: println("case 0") case let(x,y) where x == -y: println("case 1") case let(x,y): println("case 2") } // continue 立刻停止本次循环,还会继续下一次循环 for index in 1...5{ if index == 3 { continue } println("index is \(index)") } // break 结束整个循环 不会有下次循环
for index in 1...5{ if index == 3 { break } println("index is \(index)") }
let possibleString:String? = "An optional string" let stringVale = possibleString!.hashValue // 这种方法叫选择绑定 if let sValue = possibleString { let stringValue = sValue.hashValue }
let assumedString: String! = "an test string" println(assumedString) println(assumedString.hashValue)
// continue 立刻停止本次循环,还会继续下一次循环 for index in 1...5{ if index == 3 { continue } println("index is \(index)") } // break 结束整个循环 不会有下次循环
for index in 1...5{ if index == 3 { break } println("index is \(index)") }
// fallthrough 贯穿 执行了case 以后继续向下执行用这个 let integer = 5 var desc = "the number \(integer) is" switch integer{ case 2,3,5,7,11://质数 desc += "a prime number, and also" fallthrough default: desc += "an integer" } println("desc is \(desc).")
// Labeled Statements 标签语句 var score = [96,83,43,101,66,70,-5,99] for s in score{ switch s/10{ case 10: continue case 9: println("\(s) 为优秀") case 8: println("\(s) 为良好") case 7: println("\(s) 为中等") case 0: break default: println("\(s) 没及格") } }
// 要求遇到异常 就终止 所以只能用标签 var score = [96,83,43,101,66,70,-5,99] First:for s in score{ switch s/10{ case 10: continue case 9: println("\(s) 为优秀") case 8: println("\(s) 为良好") case 7: println("\(s) 为中等") case 0: break First default: println("\(s) 没及格") } } |
请发表评论