In WWDC 2019 Apple introduced SwiftUI, which is an innovative and simple way to build user interfaces across all Apple platforms.
SwiftUI is powered by Swift language and allows you to use the same tools and APIs for developing applications for macOS, iOS, iPadOS, tvOS and watchOS.
SwiftUI uses a declarative syntax so you can simply state what your user interface should do. Before the introduction of SwiftUI, developers used imperative UI which was prone to errors because it required developers to track the state of the code. For example two states of Boolean, like on or off.
In order to use SwiftUI with all the features it offers, you need to run macOS X 15 or newer and Xcode 11 or newer. Xcode 11 includes new design tools, drag and drop of controls and previews which allow you to instantly see the changes as you type your code which is a huge time saver.
If you’ve developed for iOS platform before SwiftUI, you are probably familiar with Interface Builder and storyboards.
Even though these help to visualize the code a lot, they have one huge problem. Interface Builder doesn’t know about the Swift code and vice versa. That’s why it was pretty easy before SwiftUI to delete the action in Interface Builder, and since Interface Builder didn’t care about the code, the code would still compile.
Simply put, Interface Builder and Swift were always totally separate tools, but SwiftUI changes this a big way. As a Swift-only framework, SwiftUI solves many old problems with Swift and Interface Builder.
Here are two of the most important:
- you no longer have to worry about calling non-existent functions, because user interface is checked by the compiler
- you no longer have to argue about programmatic or storyboard design, you get both at the same time
I’ve created a rather short class to show you exactly how to use SwiftUI by creating a simple contact list. This app won’t earn you and design award, but it will demonstrate the power and ease of use SwiftUI offers over old approach.
With SwiftUI, Apple wrote a whole new and pretty exciting chapter to its iOS and macOS development book. I hope you’ll like using SwiftUI as much as I do! 🙂