Skip to content

Instantly share code, notes, and snippets.

@grdsdev
Last active January 30, 2020 18:46
Show Gist options
  • Save grdsdev/d616601e5ae0bbd9815c82178f92a01b to your computer and use it in GitHub Desktop.
Save grdsdev/d616601e5ae0bbd9815c82178f92a01b to your computer and use it in GitHub Desktop.
protocol ViewConfigurable {}
extension ViewConfigurable where Self: UIView {
func configure(_ block: (Self) -> Void) -> Self {
block(self)
return self
}
}
extension UIView: ViewConfigurable {}
final View: UIView {
// Assim é mais verboso
private let label: UILabel = {
let label = UILabel()
label.text = "Hello World"
label.textColor = .red
return label
}()
// Com o configure, fica menos verboso.
private let label = UILabel().configure {
$0.text = "Hello World"
$0.textColor = .red
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment