From: acevest Date: Tue, 7 Mar 2017 10:46:16 +0000 (+0800) Subject: ... X-Git-Url: http://zhaoyanbai.com/repos/dnssec-signzone.html?a=commitdiff_plain;h=62aa15afe99be3e4c7ef6da48ec9138eaade673a;p=acecode.git ... --- diff --git a/learn/AcePlay/AcePlay.playground/Pages/Functions.xcplaygroundpage/Contents.swift b/learn/AcePlay/AcePlay.playground/Pages/Functions.xcplaygroundpage/Contents.swift index 3dbad16..ca35bc2 100644 --- a/learn/AcePlay/AcePlay.playground/Pages/Functions.xcplaygroundpage/Contents.swift +++ b/learn/AcePlay/AcePlay.playground/Pages/Functions.xcplaygroundpage/Contents.swift @@ -85,7 +85,7 @@ func Sum(_ numbers: Int...) -> Int { 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) } @@ -104,3 +104,99 @@ swapTwoInts(&IntA, &IntB) print("IntA: \(IntA) IntB: \(IntB)") +// 函数类型 +func addFunc(_ a: Int, _ b: Int) -> Int { + print(#function) + return a+b +} + +func mulFunc(_ a: Int, _ b: Int) -> Int { + print(#file, #function, #line) + return a*b +} + + +var someMathFunc: (Int, Int) -> Int +someMathFunc = addFunc +someMathFunc(1, 2) + +someMathFunc = mulFunc +someMathFunc(1, 2) + + +typealias MathFuncType = (Int, Int) -> Int +func printMathFuncResult(_ mathFunc: MathFuncType, _ a: Int, _ b: Int) -> Void { + print("Result: \(mathFunc(a, b))") +} + +printMathFuncResult(addFunc, 3, 4) +printMathFuncResult(mulFunc, 3, 4) + + + +// 函数类型作为返回值 +typealias StepFuncType = (inout Int) -> Int + +func stepForward(_ input: inout Int) -> Int { + input += 1 + print("step forward to \(input)") + return input +} + +func stepBackward(_ input: inout Int) -> Int { + input -= 1 + print("step backward to \(input)") + return input +} + +func chooseStepFunc(forward: Bool) -> StepFuncType { + if(forward) { + return stepForward + } + + return stepBackward +} + +var currentValue = Int(arc4random_uniform(20)) +print("current value: \(currentValue)") +while currentValue != 10 { + let moveTo: StepFuncType = chooseStepFunc(forward: currentValue < 10) + _ = moveTo(¤tValue) +} + + +func getRandomInt(_ n: UInt32) -> Int { + var r: Int = Int(arc4random_uniform(n)) + r *= arc4random_uniform(2) == 0 ? -1 : 1 + return r; +} + +// 嵌套函数 +func chooseStepFuncV2(forward: Bool) -> (inout Int) -> Void { + func stepForwardV2(_ input: inout Int) -> Void { + input += 1 + print("step forward to \(input)") + } + + func stepBackwardV2(_ input: inout Int) -> Void { + input -= 1 + print("step backward to \(input)") + } + + if forward { + return stepForwardV2 + } + + return stepBackwardV2 +} + +currentValue = getRandomInt(10) +print("current value: \(currentValue)") +while currentValue != 0 { + let moveTo = chooseStepFuncV2(forward: currentValue < 0) + moveTo(¤tValue) +} + + + + diff --git a/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate b/learn/AcePlay/AcePlay.playground/playground.xcworkspace/xcuserdata/Ace.xcuserdatad/UserInterfaceState.xcuserstate index 3a2a549..fa0d014 100644 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