β β β The super easy and generic builder pattern. It's easy if you try πͺ.
Initialize UIView build set its properties. π π
let label:UIView = .build{
$0.backgroundColor = .red
$0.clipsToBounds = true
}
π π This is equivalent to: π π
let label: UIView = {
let view = UIView()
view.backgroundColor = .red
view.clipsToBounds = true
return view
}()
-
let view = UIView.build { // Example 1 $0.color = .red }
let view:UIView = .build { (view) in // Example 2 view.color = .red }
let view:UIView = .build { // Example 2 $0.color = .red }
-
You can use
build
to all ofclass and structure
. Just make extensions. Init implementation is required for the class, not required for the structureextension UserType: EKBuilder {} let instance:UserType = .build{ $0.value = "it's easy if you try!" }
-
class Tesla: EKBuilder { var type:String! var count:Int! required init() {} } struct Audi:EKBuilder { var type:String! var count:Int! } final class ViewController: UIViewController { let tesla:Tesla = .build { $0.type = "Model X" $0.count = 10 } let audi = Audi.build { $0.type = "R8" $0.count = 10 } override func viewDidLoad() { super.viewDidLoad() print(tesla.type, tesla.count) print(audi.type, audi.count) } }
Add the following entry to your Podfile:
pod 'EKBuilder'
Then run pod install
.
Don't forget to import EKBuilder
in every file you'd like to use Hero.
Add the following entry to your Cartfile
:
github "erikkamalov/EKBuilder"
Then run carthage update
.
If this is your first time using Carthage in the project, you'll need to go through some additional steps as explained over at Carthage.
EKBuilder is under MIT license. See the LICENSE file for more info.