Configurable protocol: setup objects easy via closures
This tiny extension provides the approach to simplify and significantly increase visual perception of the code, to make it more structured, to segregate logic blocks and to get rid of local vars waste:
Before:
override func loadView() {
let view = UITextView(frame: .zero)
view.backgroundColor = UIColor(white: 1.0, alpha: 0.1)
view.isUserInteractionEnabled = false
view.font = UIFont.monospacedDigitSystemFont(ofSize: 8.0, weight: UIFont.Weight.regular)
view.layer.shadowColor = UIColor.white.cgColor
view.layer.shadowRadius = 4.0
view.layer.shadowOpacity = 0.9
view.layer.shadowOffset = .zero
view.layer.masksToBounds = false
self.view = view
}
After:
override func loadView() {
view = UITextView(frame: .zero).configure { view in
view.backgroundColor = UIColor(white: 1.0, alpha: 0.1)
view.isUserInteractionEnabled = false
view.font = UIFont.monospacedDigitSystemFont(ofSize: 8.0, weight: UIFont.Weight.regular)
view.layer.configure {
$0.shadowColor = UIColor.white.cgColor
$0.shadowRadius = 4.0
$0.shadowOpacity = 0.9
$0.shadowOffset = .zero
$0.masksToBounds = false
}
}
}
Configurable contains 2 protocols: Configurable
and MutableConfigurable
.
Both these protocols provide func configure(_:)
which takes a configuration closure as a parameter. The difference between them is:
- The
Configurable
protocol is dedicated for using with Swift and ObjC classes, as it passesSelf
into the configuration closure. - The
MutableConfigurable
protocol is dedicated for using with Swift Value Types (basic types, structs, enums, ...), as it passesinout Self
into the configuration closure.
let view = UIView(frame: .zero).configure { view in
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .green
view.layer.configure {
$0.cornerRadius = 5.0
$0.masksToBounds = true
}
}
struct MyModelItem: MutableConfigurable {
var itemID: Int = 0
var name: String = ""
// ....
}
let myItem = MyModelItem().configure {
$0.itemID = 123
$0.name = "name"
// ....
}
- Swift 4.2
Configurable is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Configurable"
To install Configurable add a dependency to your Cartfile:
github "jormungand/Configurable"
Import installed modules in your source files
import Configurable
Ilya Stroganov, ilya.stroganov@gmail.com
Linkedin: https://www.linkedin.com/in/ilyastroganov/
This project is licensed under the MIT License - see the LICENSE file for details