티스토리 뷰
Super sweet systactic sugar for Swift initializers 인 Then을 사용해보자.
GitHub - devxoul/Then: ✨ Super sweet syntactic sugar for Swift initializers
✨ Super sweet syntactic sugar for Swift initializers - GitHub - devxoul/Then: ✨ Super sweet syntactic sugar for Swift initializers
github.com
CocoaPods과 SPM으로 추가 가능하다.
사용법은 매우 간단하다.
원래 이런 식으로 초기화했던 것을
let label: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.textColor = .black
label.text = "Hello, World!"
return label
}()
이렇게 선언할 수 있다.
let label = UILabel().then {
$0.textAlignment = .center
$0.textColor = .black
$0.text = "Hello, World!"
}
UIKit 뿐만이 아니라 NSObject를 상속받는 모든 클래스에 사용 가능하다.
let queue = OperationQueue().then {
$0.maxConcurrentOperationCount = 1
}
extension MyType: Then {}
let instance = MyType().then {
$0.really = "awesome!"
}
let newFrame = oldFrame.with {
$0.size.width = 200
$0.size.height = 100
}
newFrame.width // 200
newFrame.height // 100
UserDefaults.standard.do {
$0.set("devxoul", forKey: "username")
$0.set("devxoul@gmail.com", forKey: "email")
$0.synchronize()
}
이전까지는 UIKit 초기화 할때만 사용해왔는데 do나 , with도 활용해 봐야겠다. 가독성이 좋아 보인다.
'iOS' 카테고리의 다른 글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- git
- Stack
- NaverMaps
- BOJ 10809
- 요시푸스
- Algorithm
- BOJ 10820
- github
- UICollectionView Error
- BOJ 1935
- Gradient View
- autolayout
- UICollectionView banner
- swift
- BOJ 10799
- crud
- Custom Detent
- CAGradientLayer
- BOJ 17413
- ios
- sheetPresentationController
- BOJ 17298
- BOJ 10808
- Carousel Effect
- bottom sheet
- Card CollectionVIew
- 1406 에디터
- 2023 회고
- BOJ 10866
- Sheet Height
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함