I am just shifted from Android base to ios, looking for builder pattern get and set method in swift unable to find anything like it. Found following only
var ptype : String? {
get{
return self.ptype
}set (ptype) {
self.ptype = ptype
}
}
The following tip looks like what you wanted https://github.com/vincent-pradeilles/swift-tips#implementing-the-builder-pattern-with-keypaths. I'll copy code here for quick looking.
protocol With {}
extension With where Self: AnyObject {
@discardableResult
func with<T>(_ property: ReferenceWritableKeyPath<Self, T>, setTo value: T) -> Self {
self[keyPath: property] = value
return self
}
}
extension UIView: With {}
let view = UIView()
let label = UILabel()
.with(\.textColor, setTo: .red)
.with(\.text, setTo: "Foo")
.with(\.textAlignment, setTo: .right)
.with(\.layer.cornerRadius, setTo: 5)
view.addSubview(label)