Instruction
SwiftUI
SwiftUI is Apple’s new declarative UI framework for building apps across all of their platforms. Unlike UIKit and AppKit, which are based on Objective-C, SwiftUI is built for Swift using modern programming paradigms. One of these is moving away from inheritance and using protocols to build your views instead.
In Objective-C, all views are subclassed from UIView, which in turn is a subclass of NSObject — the base class for all objects in Objective-C. This meant your view inherited all the behavior from UIView automatically. With SwiftUI, this is no longer the case.
SwiftUI Views
In SwiftUI, all views conform to the View protocol. View represents a part of your user interface. It requires a single property: body, which returns another View. This allows you to create lightweight views that you can build on top of each other.
And because it’s a protocol, you can conform any type to a View using an extension — even types you don’t own. SwiftUI contains a number of views you can use to create your own views, such as List, Text, Image and Button. For example, if you wanted to create a view for a Person, you could write:
HStack {
Text(person.name)
Text(person.age)
}
This uses an HStack to lay out two Text views, one on top of the other.
Views have a number of modifiers available to them. Thanks to protocol extensions, they even have accessibility features built right in.
Note: For more information on SwiftUI itself, check out the awesome SwiftUI By Tutorials book and the many incredible SwiftUI articles on Kodeco.