Declarative UIKit for iOS apps
protocol DHDeclarable
declaredWith
@discardableResult func declaredWith(_ then: (_ instance: Self) -> Void) -> Self {
then(self)
return self
}
@discardableResult
silences a warning if the return object is not used.DHDeclarable
let stackView = UIStackView()
stackView.axis = .vertical
stackView.distribution = .fill
stackView.spacing = 10
stackView.addArrangedSubview(...)
stackView.addArrangedSubview(...)
stackView.addArrangedSubview(...)
return stackView
DHDeclarable
return UIStackView().declaredWith { stack in
stack.axis = .vertical
stack.distribution = .fill
stack.spacing = 10
stack.addArrangedSubview(...)
stack.addArrangedSubview(...)
stack.addArrangedSubview(...)
}
declaredWith
provides
UIStackView()
is called firstdeclaredWith
is calledDHDeclarable
?UIKit
code a little easier than a traditional “linear” approach.UIKit
, and SwiftUI
DHDeclarable
with UIKit
objects, all code can be converted to traditional programatic code, it does not seek to replace UIKit
in any way. It builds upon it.UIKit
, and at it’s core it is one function: declaredWith
DHDeclarable
in the next article: Building on the Basics, or view the map.