Swift09 - ν΄λ‘μ
Closures
Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.
ν΄λ‘μ λ?
ν΄λ‘μ λ μ€νκ°λ₯ν μ½λ λΈλμ λλ€.
ν¨μμ λ€λ₯΄κ² μ΄λ¦μ μλ νμνμ§λ μμ§λ§, 맀κ°λ³μ μ λ¬κ³Ό λ°ν κ°μ΄ μ‘΄μ¬ ν μ μλ€λ μ μ΄ λμΌν©λλ€.
ν¨μλ μ΄λ¦μ΄ μλ ν΄λ‘μ μ λλ€.
μΌκΈκ°μ²΄λ‘ μ λ¬μΈμ, λ³μ, μμ λ±μ μ μ₯ λ° μ λ¬μ΄ κ°λ₯ν©λλ€.
ν΄λ‘μ λ ν¨μμ μ λ¬μΈμμΈ μ½λ°±ν¨μλ‘ μ£Όλ‘ μ¬μ©λλ€
κΈ°λ³Έ ν΄λ‘μ λ¬Έλ²
ν΄λ‘μ λ μ€κ΄νΈ { }λ‘ κ°μΈμ Έμμ΅λλ€.
κ΄νΈλ₯Ό μ΄μ©ν΄ νλΌλ―Έν°λ₯Ό μ μν©λλ€.
-> μ μ΄μ©ν΄ λ°ν νμ μ λͺ μν©λλ€.
"in" ν€μλλ₯Ό μ΄μ©ν΄ μ€ν μ½λμ λΆλ¦¬ν©λλ€.
{ (맀κ°λ³μ λͺ©λ‘) -> λ°ννμ
in
μ€ν μ½λ
}
κΈ°λ³Έ ν΄λ‘μ νν
// sumClosure μμμ ν΄λ‘μ λ₯Ό ν λΉ
let sumClosure: (Int, Int) -> Int = { (a: Int, b: Int) in
return a + b
}
let sumResult: Int = sumClosure(1, 2)
print(sumResult) // 3
ν¨μ vs ν΄λ‘μ
let sumClosure: (Int, Int) -> Int = { (a: Int, b: Int) in
return a + b
}
func sumFunction(a: Int, b: Int) -> Int {
return a + b
}
var result1: Int = sumClosure(3,4) // 7
var result2: Int = sumFunction(a:1, b:3) // 4
ν΄λ‘μ μΆμ½1 : νν ν΄λ‘μ , trailing closure
ν΄λ‘μ κ° ν¨μμ λ§μ§λ§ μ λ¬μΈμμΌλ, λ§μ§λ§ 맀κ°λ³μ μ΄λ¦μ μλ΅ν ν ν¨μ μκ΄νΈ μΈλΆμ ν΄λ‘μ λ₯Ό ꡬν
// ν΄λ‘μ λ₯Ό 맀κ°λ³μλ‘ κ°λ ν¨μ calculated(a:b:method:)
func calculate(a: Int, b: Int, method: (Int, Int) -> Int) -> Int {
return method(a, b)
}
// ν¨μ κ²°κ³Όκ°μ μ μ₯ν λ³μ
var result: Int
// νν ν΄λ‘μ
result = calculate(a: 10, b: 10) { (left: Int, right: Int) -> Int in
return left + right
}
print(result) // 20
ν΄λ‘μ μΆμ½2 : ν΄λ‘μ λ°ννμ μλ΅
calculate(a:b:method:) ν¨μμ method 맀κ°λ³μλ Int νμ μ λ°νν κ²μ΄λΌλ μ¬μ€μ μ»΄νμΌλ¬λ μκΈ° λλ¬Έμ κ΅³μ΄ ν΄λ‘μ μμ λ°ννμ μ λͺ μν΄ μ£Όμ§ μμλ λ©λλ€. λμ in ν€μλλ μλ΅ν μ μμ΅λλ€
// ν΄λ‘μ λ₯Ό 맀κ°λ³μλ‘ κ°λ ν¨μ calculated(a:b:method:)
func calculate(a: Int, b: Int, method: (Int, Int) -> Int) -> Int {
return method(a, b)
}
// ν¨μ κ²°κ³Όκ°μ μ μ₯ν λ³μ
var result: Int
// ν΄λ‘μ μ λ°ννμ
μλ΅
result = calculate(a: 10, b: 10, method: { (left: Int, right: Int) in
return left + right
})
print(result) // 20
ν΄λ‘μ μΆμ½3 : ν΄λ‘μ λ¨μΆ μΈμμ΄λ¦
ν΄λ‘μ μ 맀κ°λ³μ μ΄λ¦μ΄ κ΅³μ΄ λΆνμνλ€λ©΄ λ¨μΆ μΈμμ΄λ¦μ νμ©ν μ μμ΅λλ€. λ¨μΆ μΈμμ΄λ¦μ ν΄λ‘μ μ 맀κ°λ³μμ μμλλ‘ $0, $1, $2... μ²λΌ νν
// ν΄λ‘μ λ₯Ό 맀κ°λ³μλ‘ κ°λ ν¨μ calculated(a:b:method:)
func calculate(a: Int, b: Int, method: (Int, Int) -> Int) -> Int {
return method(a, b)
}
// ν¨μ κ²°κ³Όκ°μ μ μ₯ν λ³μ
var result: Int
// ν΄λ‘μ μ λ¨μΆ μΈμμ΄λ¦
result = calculate(a: 10, b: 10, method: {
return $0 + $1
})
print(result) // 20
ν΄λ‘μ μΆμ½4 : μμμ λ°ν νν
ν΄λ‘μ κ° λ°ννλ κ°μ΄ μλ€λ©΄ ν΄λ‘μ μ λ§μ§λ§ μ€μ κ²°κ³Όκ°μ μμμ μΌλ‘ λ°νκ°μΌλ‘ μ·¨κΈν©λλ€.
// ν΄λ‘μ λ₯Ό 맀κ°λ³μλ‘ κ°λ ν¨μ calculated(a:b:method:)
func calculate(a: Int, b: Int, method: (Int, Int) -> Int) -> Int {
return method(a, b)
}
// ν¨μ κ²°κ³Όκ°μ μ μ₯ν λ³μ
var result: Int
// ν΄λ‘μ μμμ λ°ν
result = calculate(a: 10, b: 10) {
$0 + $1
}
print(result) // 20
ν΄λ‘μ μΆμ½ λΉκ΅
//μΆμ½ μ
result = calculate(a: 10, b: 10, method: { (left: Int, right: Int) -> Int in
return left + right
})
//μΆμ½ ν
result = calculate(a: 10, b: 10) { $0 + $1 }
print(result) // 20