Swift 易忽略的笔记 4):Function & Closure
来源:互联网 发布:点对点网络测速工具 编辑:程序博客网 时间:2024/05/02 00:59
1. func
External Parameter Names: 方便阅读,类似OC的method,每个变量前都有说明符,赞
func join(string s1: String, toString s2: String, withJoiner joiner: String) -> String { return s1 + joiner + s2}
join(string: "hello", toString: "world", withJoiner: ", ")// returns "hello, world”
func containsCharacter(#string: String, #characterToFind: Character) -> Bool { for character in string { if character == characterToFind { return true } } return false}
let containsAVee = containsCharacter(string: "aardvark", characterToFind: "v")// containsAVee equals true, because "aardvark" contains a "v”
Variadic Parameters:
func arithmeticMean(numbers: Double...) -> Double { var total: Double = 0 for number in numbers { total += number } return total / Double(numbers.count)}arithmeticMean(1, 2, 3, 4, 5)// returns 3.0, which is the arithmetic mean of these five numbersarithmeticMean(3, 8, 19)// returns 10.0, which is the arithmetic mean of these three numbers”
“Constant and Variable Parameters”
func alignRight(var string: String, count: Int, pad: Character) -> String { let amountToPad = count - countElements(string) for _ in 1...amountToPad { string = pad + string } return string}let originalString = "hello"let paddedString = alignRight(originalString, 10, "-")// paddedString is equal to "-----hello"// originalString is still equal to "hello”
In-Out Parameters:相当于按引用传递
func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA}”
var someInt = 3var anotherInt = 107swapTwoInts(&someInt, &anotherInt)println("someInt is now \(someInt), and anotherInt is now \(anotherInt)")// prints "someInt is now 107, and anotherInt is now 3”
Function Type
var mathFunction: (Int, Int) -> Int = addTwoInts”
function type as return type
func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward}”
Nested Function:
func chooseStepFunction(backwards: Bool) -> (Int) -> Int { func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } return backwards ? stepBackward : stepForward}var currentValue = -4let moveNearerToZero = chooseStepFunction(currentValue > 0)// moveNearerToZero now refers to the nested stepForward() functionwhile currentValue != 0 { println("\(currentValue)... ") currentValue = moveNearerToZero(currentValue)}println("zero!")// -4...// -3...// -2...”
2. Closure
Closure Expression Syntax
{ (parameters) -> return type in statements}
reversed = sort(names, { (s1: String, s2: String) -> Bool in return s1 > s2 })
“Inferring Type From Context” 也就是说,作为函数的参数时候,这些类型可以省略
reversed = sort(names, { s1, s2 in return s1 > s2 } )
reversed = sort(names, { $0 > $1 } )
reversed = sort(names, >)
Trailing Closures:
func someFunctionThatTakesAClosure(closure: () -> ()) { // function body goes here} // here's how you call this function without using a trailing closure: someFunctionThatTakesAClosure({ // closure's body goes here }) // here's how you call this function with a trailing closure instead: someFunctionThatTakesAClosure() { // trailing closure's body goes here}
reversed = sort(names) { $0 > $1 }
例子:
let digitNames = [ 0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"]let numbers = [16, 58, 510]
let strings = numbers.map { (var number) -> String in var output = "" while number > 0 { output = digitNames[number % 10]! + output number /= 10 } return output}// strings is inferred to be of type String[]// its value is ["OneSix", "FiveEight", "FiveOneZero"]
0 0
- Swift 易忽略的笔记 4):Function & Closure
- Swift 易忽略的笔记 1)
- swift - function 与closure的同异
- Swift Function & Closure
- Swift 易忽略的笔记 2):String
- Swift 易忽略的笔记 3): Array & Dictionary
- Swift 易忽略的笔记 6):Methods & Subscripts
- Swift 易忽略的笔记 8):Extensions
- Swift 易忽略的笔记 9):Protocol
- swift 易忽略的笔记 10):Generic
- swift 易忽略的笔记 11):Operator Functions
- swift 易忽略的笔记 12): Miscellaneous
- lua学习笔记之Lua的function、closure和upvalue
- lua学习笔记之Lua的function、closure和upvalue
- Swift 易忽略的笔记 5):Enumeration & Classes and Structs & Properties总结
- Swift 易忽略的笔记 7):Type Casting & Nested Type
- Swift中闭包(closure)的使用
- Swift闭包(Closure)
- android 利用 service 实现音乐播放
- java web学习基础(四) session对象 application对象 config对象 out对象 pageContext对象
- poj1276
- device eth0 does not seem to be present, delaying initialization
- proguard混淆带第三方库android项目-java mail
- Swift 易忽略的笔记 4):Function & Closure
- linux命令--tar
- 仿360在Launcher画面显示内存使用率的浮窗(基础版)
- 全文检索概念,Lucene大致结构
- 电子商务15条营销方式汇总
- 【树状数组】 HDOJ 1394 Minimum Inversion Number
- lua学习笔记05
- 步步为营---- MuleEsb学习(一) 扫盲篇
- 下面的代码告诉你返回的时候应该如何选择(返回的是数组还是对象)