Understanding SwiftUI

Not too long ago, if you wanted to create a user interface, iOS provided lots of different solutions. These solutions worked in a variety of different ways and each held their own pain points. This meant each project could be different.

In 2019, Apple introduced an entirely new way to create user interfaces with SwiftUI. While the old ways of making UIs still exist, SwiftUI streamlines UI development into a process that’s easy to understand, intuitive, and most of all, it’s fun.

SwiftUI works by composing various views together. Out of the box, SwiftUI provides all the various elements that you use in your app. There are buttons, text fields, labels, and so on. You can even design your own. Interfaces are created by placing views in horizontal and vertical stacks that automatically adjust based on the size of the device.

These views are lightweight. This means that they render fast on screen. With lightweight views, you can edit your interface, and in realtime, see the results onscreen. This provides for rapid iteration and experimentation.

Reflecting the Data

SwiftUI represents a current trend known as declarative programming. With declarative programming, your user interface reflects the state of your data. The framework observes special variables and when those variables change, the user interface is automatically updated.

For example, you may have a screen that simply reads, “Hello Ray”. If the name Ray was a special variable and changed to Sam, the interface would automatically update to read “Hello Sam”.

Other frameworks are known as imperative frameworks. With these frameworks, you must manage everything on the screen. While this gives you precise control, it also allows for potential bugs where the UI falls out of sync with data. UIKit, the old user interface toolkit, is an imperative framework and still extensively used throughout iOS.

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo: Making your First SwiftUI App