Swift ) Closure
μ€λμ μ€ν°λ μΉκ΅¬λ€κ³Ό ν¨κ» ν΄λ‘μ μ λν λ΄μ©μ 곡λΆν΄λ³΄μμ΅λλ€ !
πClosure λ?
- μΌμ κΈ°λ₯μ νλ μ½λλ₯Ό νλμ λΈλ‘μΌλ‘ λͺ¨μλμ κ²
- func ν€μλλ₯Ό μ¬μ©νμ¬ λ§λ€μλ ν¨μ : named Closure
- μΌλ°μ μΈ ν΄λ‘μ : Unnamed Closureλ₯Ό μ§μΉ
{ (Parameters) -> Return Type in
μ€ν ꡬ문
}
πΏ1κΈ κ°μ²΄λ‘μμ νΉμ§
- ν΄λ‘μ λ₯Ό λ³μλ μμμ λμ
ν μ μμ΅λλ€.
- ν¨μμ νλΌλ―Έν° νμ
μΌλ‘ ν΄λ‘μ λ₯Ό μ λ¬ν μ μμ΅λλ€.
- ν¨μμ λ°ν νμ μΌλ‘ ν΄λ‘μ λ₯Ό μ¬μ©ν μ μμ΅λλ€.
μΌλ° ν¨μμ Closureμ μ¬μ© λ°©μμ μ°¨μ΄
: Closureλ ν¨μλ₯Ό func ν€μλλ‘ μ μΈνλ κ²μ΄ μλλΌ, ν¨μλ₯Ό λ³μμ μ μΈνλ ννλ₯Ό μ·¨νκ³ μμ΅λλ€.
- μΌλ°μ μΈ ν¨μ μ¬μ©
var counter = 0
func addCounter() {
counter += 1
}
addCounter()
addCounter()
print(counter) // κ²°κ³Ό 2
μΌλ°μ μΈ ν¨μλ λ€μκ³Ό κ°μ΄ ν¨μλͺ μ μ€μ (addCounter)νκ³ , ν΄λΉ ν¨μλͺ μΌλ‘ ν¨μλ₯Ό νΈμΆνλ ννλ₯Ό μ·¨ν©λλ€.
- Closure μ¬μ©
var counter = 0
let addCounter = {
counter += 1
}
addCounter()
addCounter()
print(counter) // κ²°κ³Ό 2
Closureλ λ³μμ κ°μ μ μΈνλ λμ μ λ³μμ ν¨μ λ₯Ό μ μΈν©λλ€. μ¬κΈ°μλ addCounter λ³μμ ν¨μλ₯Ό μ μΈνμμ΅λλ€. κ·Έλ¦¬κ³ μ΄ λ³μλ ν¨μμ²λΌ νΈμΆμ ν μ μμ΅λλ€.(addCounter() νν)
- Inline Closure : ν¨μλ‘ λ°λ‘ μ μλ ννκ° μλ μΈμλ‘ λ€μ΄κ° μλ ννμ ν΄λ‘μ
let reverseNames = names.sorted(by: { (s1: String, s2: String) -> Bool in return s1 > s2})
μμ κ°μ ννμμ ν΄λ‘μ μ μΆμ½ ννμ΄ κ°λ₯ν©λλ€
νμ , λ°νν€μλ, μΈμμ΄λ¦μ μλ΅ν μ μμ΅λλ€
// νμ
μλ΅ : μ½λμ λͺ¨νΈμ±μ νΌνκΈ° μν΄ νμ
μ λͺ
μνλ κ²μ΄ μ’μλλ μλ€.
let reverseNames = names.sorted(by: {s1, s2 in return s1 > s2})
// λ°ν ν€μλ μλ΅
let reverseNames = names.sorted(by: {s1, s2 in s1 > s2})
// μΈμ κ°μ μΆμ½ : $0 λΆν° μμλλ‘
let reversedNames = names.sorted(by: { $0 > $1 })
// μ°μ°μ λ©μλ : μ°μ°μλ§ λ¨κΈΈ μ μμ
let reversedNames = names.sorted(by: > )
- Trailing closure (νν ν΄λ‘μ ): ν¨μμ 맀κ°λ³μ λ§μ§λ§μΌλ‘ μ λ¬λλ ν΄λ‘μ
μΈμλ‘ ν΄λ‘μ λ₯Ό λ£κΈ°κ° κΈΈλ€λ©΄ νν ν΄λ‘μ λ₯Ό μ¬μ©νμ¬ ν¨μμ λ€μ ννν μ μμ΅λλ€ !
λ§μ½ ν΄λ‘μ λ΄λΆμ μ¬λ¬ μ€μ μ€ν μ½λκ° μλ€λ©΄ ννν΄λ‘μ λ₯Ό μ¬μ©ν΄ κ°λ
μ±μ λμ΄λ κ²μ΄ μ’λ€κ³ ν©λλ€
λ§μ§λ§ μ λ¬ μΈμλ‘ μ λ¬λλ ν΄λ‘μ μλ§ ν΄λΉλλ€λ κ²μ κΈ°μ΅ !
let reversedNames = names.sorted { $0 > $1 }
let reversedNames = names.sorted { (s1: String, s2: String) -> Bool in return s1 > s2 }
μμ - Closureλ₯Ό νμ©ν λ°μ΄ν° μ λ¬
- λ°μ΄ν°λ₯Ό μ λ¬ λ°λ VCμ νλ‘νΌν° μ μΈ
var categoryId: Int
var titleText: String
var iconImageId: Int
- λ°μ΄ν°λ₯Ό μ λ¬νλ Cellμμ ν΄λ‘μ μ μΈ -> Cell μμ μλ λ²νΌμ λλ μ λ ν΄λ‘μ κ° μ€ν λλλ‘ !
var presentEditCategoryClosure: (() -> Void)?
private func bind() {
editButton.rx.tap
.bind(onNext: { [weak self] in
self?.presentEditCategoryClosure?()
})
.disposed(by: disposeBag)
}
- CollectionViewDataSource μμ Cell μ λ°μ΄ν°λ₯Ό μ§μ ν λ μ€μ ꡬν μ½λ μμ±
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(forIndexPath: indexPath) as CategoryCollectionViewCell
cell.configure(type: .manage)
cell.update(data: categories[indexPath.row])
cell.presentEditCategoryClosure = {
let categoryId = self.categories[indexPath.row].id ?? 0
let titleText = self.categories[indexPath.row].title ?? ""
let imageId = self.categories[indexPath.row].imageId ?? 0
self.navigationController?.pushViewController(editCategory, animated: true)
}
- Alert : νμΈ λ²νΌμ λλ₯Ό λ Closure μ¬μ©
extension UIViewController {
func makeAlert(title: String,
message: String,
okAction: ((UIAlertAction) -> Void)? = nil,
completion : (() -> Void)? = nil) {
let alertViewController = UIAlertController(title: title,
message: message,
preferredStyle: .alert)
let okAction = UIAlertAction(title: "νμΈ", style: .default, handler: okAction)
alertViewController.addAction(okAction)
self.present(alertViewController, animated: true, completion: completion)
}
}
self.makeAlert(title: "μΉ΄ν
κ³ λ¦¬ μμ ", message: "μΉ΄ν
κ³ λ¦¬ μμ μ±κ³΅", okAction: { [weak self] _ in
self?.titleText = self?.categoryTitleTextField.text ?? ""
self?.sendEditData?()
self?.navigationController?.popViewController(animated: true)
})
πμ°Έκ³ ν μ¬μ΄νΈ
μ€λμ Swift μμ (Closure)
ν΄λ‘μ (Closure)λ?
medium.com
Swift Closure
Swiftμ closureμ λν΄ μμλ΄ λλ€.
hcn1519.github.io
π κ°μ΄ μ€ν°λν μΉκ΅¬λ€μ ν°μ€ν 리
[iOS/Swift] Closureμ λν΄ μμ보μ
λ€μ΄κ°λ©° ν΄λ‘μ λ μ΄λ₯Έλ° μ΄λ¦ μλ ν¨μ, “μ΅λͺ ν¨μ"λ‘ λΆλ¦¬λ μ½λλΈλμ λλ€. μ΄λ² κ²μκΈμμλ iOS κ°λ°μ νλ©° μ ~λ§μ λ§ λ§μ΄ μ¬μ©νλ λ¬Έλ²μΈ ν΄λ‘μ μ λν΄ νꡬν΄λ³΄λ μκ°μ κ°μ§
osoomoovo.tistory.com
π μΆκ°μ μΌλ‘ 곡λΆν λ§ν λ΄μ©
λ€μμλ Escaping Closureμ Completion Handlerμ λν λ΄μ©λ μ λ¦¬ν΄ λ³΄κ² μ΅λλΉ
[SWIFT] Escaping Closure(νμΆ ν΄λ‘μ )
ν΄λ‘μ μ κΈ°λ³Έμ λν΄ ν¬μ€ν μ ν μ μ΄ μμλλ°μ. μ΄λ²μλ ν΄λ‘μ νμ©μ λν΄ ν¬μ€ν μ ν΄λ³Όκ²μ. μ΄μ ν¬μ€ν μ μ보μ λΆμ λ³΄κ³ μ€μλ©΄ μ’μ κ² κ°μμ. [SWIFT] Closure(ν΄λ‘μ ) (1/2) μ€λμ
dongminyoon.tistory.com
[iOS] Completion Handler
Completion Handler λ³Έ λ¬Έμμλ νμμ 곡λΆλ₯Ό μ§ννλ©° νλ² μ λ¦¬κ° νμνλ€κ³ μκ°νλ Completion Handler μ λν λ΄μ©μ κΈ°μ¬νλ€. Prerequisite Completion Handler κ°λ μ μλ©΄ μμλ‘ μ΄λ €μ΄ κ°λ μ΄λ€....
duwjdtn11.tistory.com
Swift Escaping Closure μ΄ν΄νκΈ°
Swiftμ Escaping Closureμ λν΄ μμλ΄ λλ€.
hcn1519.github.io