]> Zhao Yanbai Git Server - acecode.git/commitdiff
...
authorAceVest <zhaoyanbai@126.com>
Mon, 6 Mar 2017 16:02:04 +0000 (00:02 +0800)
committerAceVest <zhaoyanbai@126.com>
Mon, 6 Mar 2017 16:02:04 +0000 (00:02 +0800)
learn/AcePlay/AcePlay.playground/Pages/ControlFlow.xcplaygroundpage/Contents.swift
learn/AcePlay/AcePlay.playground/Pages/Functions.xcplaygroundpage/Contents.swift
learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate

index 781d883b7d0b505f434d618390f8e80fee7f3530..a52af6a13a4d21eaf1d3e683f471b376f9a63512 100644 (file)
@@ -63,7 +63,9 @@ case 0:
 case 1..<5:
     naturalCount = "a few"
 case 5:
-    fallthrough                 // 默认不走到下一个case
+    // 默认不走到下一个case
+    // fallthrough关键字不会检查它下一个将会落入执行的 case 中的匹配条件
+    fallthrough
 case 6..<12:
     naturalCount = "several"
 case 12..<100:
@@ -74,4 +76,146 @@ default:
     naturalCount = "many"
 }
 
-print("There are \(naturalCount) \(countedTings) [\(approximateCount)]")
\ No newline at end of file
+print("There are \(naturalCount) \(countedTings) [\(approximateCount)]")
+
+
+var x = Int(arc4random_uniform(100))
+var y = Int(arc4random_uniform(100))
+let s = arc4random_uniform(4)
+
+if s & 1 != 0 {
+    x = -x
+}
+
+if s & 2 != 0 {
+    y = -y
+}
+
+let somePoint = (x, y)
+
+switch somePoint {
+case (0, 0) :
+    print("(0, 0) is at the origin")
+    
+case (_, 0) :
+    print("(\(x), 0) is on the x-axis")
+
+case (0, _) :
+    print("(0, \(y)) is on the y-axis")
+
+case (-50...50, -50...50) :
+    print("(\(x), \(y)) is inside the box")
+    
+default:
+    print("(\(x), \(y)) is outside the box")
+}
+
+
+// 值绑定 和 where 语句
+switch somePoint {
+case (let x, 0) :
+    print("on the x-axis with a x value of \(x)")
+
+case (0, let y) :
+    print("on the y-axis with a y value of \(y)")
+    
+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("somewhere else at (\(x), \(y))")
+}
+
+let alphabet = "abcdefghijklmnopqrstuvwxyz"
+let rc = alphabet[alphabet.index(alphabet.startIndex, offsetBy: String.IndexDistance(arc4random_uniform(UInt32(alphabet.characters.count))))]
+
+// 复合匹配
+switch  rc {
+case "a", "e", "i", "o", "u" :
+    print("\(rc) is a vowel")
+case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n",
+     "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" :
+    print("\(rc) is a consonant")
+default:
+    print("\(rc) is not a vowel or a consonant")
+}
+
+switch somePoint {
+case (let distance, 0), (0, let distance) :
+    print("On an axis, \(distance) from the origin")
+default :
+    print("Not on an axis")
+}
+
+
+
+let finalSquare = 25
+var board = [Int](repeating:0, count: finalSquare + 1)
+
+board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
+board[04] = -10; board[19] = -11; board[22] = -02; board[24] = -08
+
+
+var diceRoll = 0
+var square = 0
+
+gameLoop: while square != finalSquare {
+    diceRoll += 1
+    
+    diceRoll %= 6
+    diceRoll += 1
+    
+    switch diceRoll {
+    case finalSquare:
+        break gameLoop      // break 会执行gameLoop标签的后一条代码也就是print("Game Over")
+    case let newSquare where square > finalSquare:
+        continue gameLoop   // continue 会执行gameLoop标签指示的代码
+    default:
+        square += 1
+        diceRoll += board[square]
+    }
+}
+
+print("Game Over")
+
+
+
+// guard
+func greet(person: [String:String]) {
+    guard let name = person["name"] else {          // guard 总是要跟一个 else
+        print("please specify the person's name")
+        return                                      // guard 代码体最终必须要退出函数
+    }
+    
+    print("Hello \(name)")
+    
+    guard let location = person["location"] else {
+        print("please specify \(name)'s location")
+        return
+    }
+    
+    print("I hope the weather is nice in \(location)")
+    
+    if #available(iOS 10, macOS 11.12, *) { // 指定代码版本 也可用于 guard 语句
+        print("that's right platform")
+    } else {
+        print("invalid platform")
+    }
+    
+    guard #available(iOS 99, *) else {
+        print("Under iOS 99 now")
+        return
+    }
+}
+
+greet(person: ["noname" : "noname"])
+greet(person: ["name" : "Ace"])
+greet(person: ["name" : "AceVest", "location":"moon"])
+
+
+
+
+
index d248acc304f233865796b76d400ce8e52ad0ecf9..3dbad16415ab4b3e0fed6a8b79b44bae0059abc5 100644 (file)
@@ -7,12 +7,19 @@ func Hello() -> Void {  // func Hello() -> () { }
     print("Hello Swift World")
 }
 
+func greetFunc(person: String) -> String {
+    let greeting = "Hello \(person)!"
+    return greeting
+}
+
 
 printLine("Functions")
 
 Hello()
+print(greetFunc(person: "Ace"))
+print(greetFunc(person: "AceVest"))
 
-printLine("Multiple Return Types")
+printLine("Multiple Return Types")  // 多重返回
 
 func minMax(_ data:Array<Int>) -> (min:Int, max:Int)? {
     guard data.count > 0 else {
@@ -36,6 +43,48 @@ if let ret = minMax(Data) {
     print("Min:", ret.min, " Max:", ret.max)
 }
 
+
+// 参数标签
+// 每个参数都有一个参数标签(argument label)和一个参数名称(parameter name)
+// 参数标签在调用函数的时候使用,调用的时候需要把函数的参数标签写在参数的前面
+// 参数名称在实现函数的时候使用
+// 参数标签要在参数名称前指定,中间以空格分隔
+// 如果一个参数有一个标签,那么在调用的时候必须使用标签来标记这个参数
+func greetFuncV2(person: String, from hometown: String) -> Void {
+    print("Hello \(person), Glad you could visit from \(hometown).")
+}
+
+greetFuncV2(person: "Ace", from: "China")
+
+
+// 忽略参数标签
+func greetFuncV3(_ person: String) -> Void {
+    print("Hello \(person)")
+}
+
+greetFuncV3("Ace")  // 不用在参数列表里写上'person:"Ace"'
+
+// 默认参数值
+func greetFuncV4(_ person: String, from hometown:String = "China") {
+    print("Hello \(person), Glad you could visit from \(hometown).")
+}
+
+greetFuncV4("Ace")
+greetFuncV4("Obama", from: "USA")
+
+// 可变参数
+// 一个函数最多只能拥有一个可变参数
+func Sum(_ numbers: Int...) -> Int {
+    var s = 0;
+    for n in numbers {
+        s += n
+    }
+
+    return s
+}
+
+print(Sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
+
 // 外部参数包能相同, 但内部参数名不能相同
 func sameExternalParameterNames( ExName a: Int, ExName b: Int) -> Int {
     return max(a, b)
@@ -52,5 +101,6 @@ func swapTwoInts(_ a: inout Int, _ b: inout Int) -> Void {
 var IntA = 10
 var IntB = 20
 swapTwoInts(&IntA, &IntB)
+print("IntA: \(IntA) IntB: \(IntB)")
 
 
index 35670e3b0b715ecb0994f30d1d0ef34db86ec9b2..3a2a549e8ecf7e0e8fd4615ed1efa6c4f8cb5e5e 100644 (file)
Binary files a/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate and b/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate differ