μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- μ΄μ§νμ
- λμ κ³νλ²
- SOPT
- SwiftUI
- HAVIT
- SwiftUI νν 리μΌ
- νλ‘κ·Έλλ¨Έμ€
- λ€μ΄λλ―Ήνλ‘κ·Έλλ°
- SwiftUI Tutorials
- GCD
- κΈ°μ΄λ¬Έλ²
- algorithm
- concurrency
- IOS
- GroupBy
- BFS
- URLSession
- binarySearch
- DynamicProgramming
- APPJAM
- κ³ λμ kit
- algoritm
- duno
- dfs
- Til
- SQL
- μ°μνμ€λΆλΆμμ΄μν©
- discardableResult
- Swift
- 0μ΄λμ΄μλκΈΈ
- Today
- Total
suvera-dev π₯¦
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)
})
πμ°Έκ³ ν μ¬μ΄νΈ
π κ°μ΄ μ€ν°λν μΉκ΅¬λ€μ ν°μ€ν 리
π μΆκ°μ μΌλ‘ 곡λΆν λ§ν λ΄μ©
λ€μμλ Escaping Closureμ Completion Handlerμ λν λ΄μ©λ μ λ¦¬ν΄ λ³΄κ² μ΅λλΉ
'Language > Swift' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Swift) νλ‘ν μ½ μ§ν₯ νλ‘κ·Έλλ° (4) | 2022.04.03 |
---|---|
Swift ) κΈ°μ΄ λ¬Έλ² 6μΌμ°¨ - Class μ Struct (2) | 2022.02.10 |
Swift ) @discardableResult λ? (4) | 2022.02.08 |
Swift ) κΈ°μ΄ λ¬Έλ² 5μΌμ°¨ - UnWrap μ΅μ λ λ³μ (0) | 2022.02.07 |
Swift ) κΈ°μ΄ λ¬Έλ² 4μΌμ°¨ - For λ°λ³΅λ¬Έ & Wildcard Pattern (0) | 2022.02.07 |