Declarative UIKit for iOS apps
protocol DHDeclarabledeclaredWith@discardableResult func declaredWith(_ then: (_ instance: Self) -> Void) -> Self {
then(self)
return self
}
@discardableResult silences a warning if the return object is not used.DHDeclarablelet stackView = UIStackView()
stackView.axis = .vertical
stackView.distribution = .fill
stackView.spacing = 10
stackView.addArrangedSubview(...)
stackView.addArrangedSubview(...)
stackView.addArrangedSubview(...)
return stackView
DHDeclarablereturn 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 SwiftUIDHDeclarable 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: declaredWithDHDeclarable in the next article: Building on the Basics, or view the map.