SwiftyBuilder is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SwiftyBuilder'
https://github.com/kingreza/Swift-Builder
- before
let view: UIView = {
var view = UIView()
view.backgroundColor = .black
view.alpha = .zero
return view
}()
- after
let view = UIView()
.builder
.backgroundColor(.black)
.alpha(.zero)
.build()
- the value types
let array = [""]
.builder
.do {
$0.append("Hello")
}
.build()
NSObject
subclasses
let button = UIButton()
.builder
.apply {
$0.setTitle("Hello, World", for: .normal)
}
.build()
- before
let label = UILabel()
.builder
.font(.systemFont(ofSize: 10))
.numberOfLines(12)
.build()
print(label.numberOfLines) // 12
- extension
extension Builder where Base: UILabel {
// custom
func size(_ value: CGFloat) -> Self {
return set(\.font, value: base.font.withSize(value))
}
// intercept
func numberOfLines(_ value: Int) -> Self {
return set(\.numberOfLines, value: min(value, 10))
}
}
let label = UILabel()
.builder
.size(10)
.numberOfLines(12)
.build()
print(label.numberOfLines) // 10
- declare
struct User: Buildable {
var id: String?
var name: String?
}
typealias UserBuilder = Builder<User>
// or
extension Builder where Base == User {}
- intercept
extension UserBuilder {
func id(_ value: String) -> Self {
return set(\.id, value: "ID : \(value)")
}
}
let user = User()
.builder
.id("user")
.build()
print(user.id!) // ID : user
https://github.com/devxoul/Then https://wilson-gramer-blog.github.io/posts/2020-02-23-using-dynamic-member-lookup-implement-builder-pattern.html
SwiftyBuilder is available under the MIT license. See the LICENSE file for more info.