Created
July 5, 2015 08:57
-
-
Save masakid/9cbdfe1459b32299bab7 to your computer and use it in GitHub Desktop.
[swift]クロージャの書き方
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//クロージャ例 | |
//ノーマルな書き方 | |
let myFunc = {(a: Int, b: Int) -> Int in | |
return a+b | |
} | |
let ans = myFunc(1,2) | |
println(ans) | |
let numbers = [4,7,2,8] | |
let array = map(numbers, {(let v:Int) -> Int in | |
return v*2 | |
}) | |
println(array) | |
//map関数の後ろにクロージャを書くことも可能 | |
let array2 = map(numbers) {(let v:Int) -> Int in | |
return v*2 | |
} | |
//型宣言と戻し値のreturnを省略 | |
let array3 = map(numbers) {v in v*2} | |
//クロージャの引数は$0,$1,$2(第N引数)で省略可能 | |
let array4 = map(numbers) {$0*2} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment