λ©λͺ¨λ¦¬ κ΄λ¦¬ λλ¬Έμ νμ weak self λ₯Ό μ¬μ©νκ³ μμλ€.
μ’ λ₯ | μ΄λ¦ | μΊ‘μ³ |
---|---|---|
μ μ ν¨μ | O | X |
μ€μ²© ν¨μ | O | λλ¬μ¬ ν¨μ λ΄μμ μΊ‘μ³ κ°λ₯ |
ν΄λ‘μ ννμ | X | μ£Όλ³ μ»¨ν μ€νΈμμ O |
-> ν΄λ‘μ ννμμ μ΄μ©νλ©΄ κΉλν ꡬ문μ μ΄μ©ν μ μλ€.
π ν΄λ‘μ μ μΈ κ°μ§ νν
1. μ΄λ¦μ΄ μκ³ μ΄λ€ κ°λ νλνμ§ μμ - μ μν¨μ
2. μ΄λ¦μ΄ μκ³ λ€λ₯Έ ν¨μ λ΄λΆμ κ°μ νλ - μ€μ²©ν¨μ
3. μ΄λ¦μ΄ μκ³ μ£Όλ³ λ¬Έλ§₯μ λ°λΌ κ°μ νλ - μΆμ½ λ¬Έλ²
sorted(by: ) λ₯Ό μμλ‘ μ€λͺ
.
λ€λ₯Έ μΈμ΄λ€ μ²λΌ, sorted μλ μ, λ€ λ μΈμλ₯Ό λΉκ΅ν΄μ Bool κ°μ λ°ν(첫λ²μ§Έ κ°μ΄ μμ΄λ©΄ true, λλ²μ§Έ κ°μ΄ μμ΄λ©΄ false λ°ν)νλ ν΄λ‘μ λ₯Ό λ£μ΄μ£Όλ©΄ λλ€.
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
func backward(_ s1: String, _ s2: String) -> Bool {
return s1 > s2
}
var reversedNames = names.sorted(by: backward)
// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]
RxSwift μ Observable μ 리ν΄νλ€λκ° νλ 볡μ‘ν ν΄λ‘μ λ return type μ λͺ
μν΄μ€ νμκ° μλ€.
π 맀κ°λ³μ μ΄λ¦μ μ§μ νλ©΄ κ°λ³ 맀κ°λ³μλ μ¬μ© κ°λ₯!
reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in
return s1 > s2
})
// νμ
μ μΆ
reversedNames = names.sorted(by: { s1, s2 in
return s1 > s2
})
// μμμ λ°ν
reversedNames = names.sorted(by: { s1, s2 in
s1 > s2
})
// 짧μ μΈμ μ΄λ¦
reversedNames = names.sorted(by: { $0 > $1 } )
// μ°μ°μ λ©μλ : μ΄κ±΄ μ°μ°μμΈ κ²½μ°.
reversedNames = names.sorted(by: >)
reversedNames = names.sorted { $0 > $1 }
loadPicture(from: someServer) { picture in
someView.currentPicture = picture
}
loadPicture(from: someServer) { picture in
someView.currentPicture = picture
} onFailure: {
print("Couldn't download the next picture.")
}
μΊ‘μ³ μμ - μ€μ²© ν¨μ
func makeIncrementer(forIncrement amount: Int) -> () -> Int {
var runningTotal = 0
func incrementer() -> Int {
runningTotal += amount
return runningTotal
}
return incrementer
}
let incrementByTen = makeIncrementer(forIncrement: 10)
incrementByTen()
// returns a value of 10
incrementByTen()
// returns a value of 20
incrementByTen()
// returns a value of 30
// μλ‘μ΄ μ°Έμ‘°μ μ μλλ€.
let incrementBySeven = makeIncrementer(forIncrement: 7)
incrementBySeven()
// returns a value of 7
let alsoIncrementByTen = incrementByTen
alsoIncrementByTen()
// returns a value of 50
incrementByTen()
// returns a value of 60
var completionHandlers: [() -> Void] = []
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
completionHandlers.append(completionHandler)
}
func someFunctionWithNonescapingClosure(closure: () -> Void) {
closure()
}
class SomeClass {
var x = 10
func doSomething() {
someFunctionWithEscapingClosure { self.x = 100 }
// someFunctionWithEscapingClosure { [self] in x = 100 }
someFunctionWithNonescapingClosure { x = 200 }
}
}
let instance = SomeClass()
instance.doSomething()
print(instance.x)
// Prints "200"
completionHandlers.first?()
print(instance.x)
// Prints "100"
func hasElement(in array: [Int], match predicate: (Int) -> Bool) -> Bool {
return (array.lazy.filter { predicate($0) }.isEmpty == false)
}
withoutActuallyEscaping(_:do:)
ν¨μλ₯Ό μ¬μ©ν΄μ ν΄κ²°func hasElement(in array: [Int], match predicate: (Int) -> Bool) -> Bool {
return withoutActuallyEscaping(predicate, do: { escapablePredicate in
array.lazy.filter { escapablePredicate($0) }.isEmpty == false)
})
}
π assert(condition:message:file:line:)
μμ condition, message κ° μλ ν΄λ‘μ λ‘ λμ΄ μλ€.
π ν΄λ‘μ μ λν 컨벀μ
λ μ νλ κ²μ΄ μ’λ€.
μλμ²λΌ ν΄λ‘μ λ₯Ό λ°μμ μ€ννλ ν¨μμμ μΈμλ₯Ό μ λ¬ν λλ {} μ μΈμ 보λ΄μΌ νλ€. λΉμ°ν¨. ν΄λ‘μ μ.
// customersInLine is ["Alex", "Ewa", "Barry", "Daniella"]
func serve(customer customerProvider: () -> String) {
print("Now serving \(customerProvider())!")
}
serve(customer: { customersInLine.remove(at: 0) } )
// Prints "Now serving Alex!"
// customersInLine is ["Ewa", "Barry", "Daniella"]
func serve(customer customerProvider: @autoclosure () -> String) {
print("Now serving \(customerProvider())!")
}
serve(customer: customersInLine.remove(at: 0))
// ν΄λ‘μ λμ customerInLine.remove(at: 0) μ κ²°κ³Ό κ° (String) μ μ λ¬λ°κ² λλ€.
// Prints "Now serving Ewa!"
serve(customer: "hello")
// Prints "Now serving hello!"
auto closure μ΄λ©΄μ escaping closure μΌ μλ μλ€.
auto closure νμ© μμλ μ¬κΈ°μ μ°Έκ³ νλ©΄ μ’μ λ―!
https://jusung.github.io/AutoClosure/
νμΆ ν΄λ‘μ λ escaping closure λ€. closure λ ν¨μμ μΆμ½νμ΄λΌκ³ 보면 λλλ°, ν΄λΉ ν΄λ‘μ κ° ν¨μ λ΄λΆμμ μ°μ΄κ³ λλμ§ μκ³ , ν¨μ μΈλΆμμ μ°μ΄λ κ²½μ°! (νΉν, λΉλκΈ°μ μΈ μμ μ ν λ ν¨μκ° λλκ³ λ λμν΄μΌ νλ μ½λκ° μλ κ²½μ°μ μ°μΈλ€.) ν¨μ μΈλΆμμ λμ€μ μ°λ ν΄λ‘μ μ΄λ€. μΈμ λΌλ²¨ μμ @escaping ν€μλλ₯Ό μ¨μ νμνλ€.
+) 0325 μΆκ°μ μΌλ‘ μ°Ύμ λ³Έ κ².
+) 0401
μ°Έκ³
https://bbiguduk.gitbook.io/swift/language-guide-1/closures
https://github.com/JeaSungLEE/iOSInterviewquestions