SwiftUI is Apple’s new(ish) “cross-platform” UI framework becoming dominant for new development on iOS, and now viable on macOS.
I started writing Swift in 2014 when it just came out (here’s my first non-toy app on GitHub), but I didn’t yet use SwiftUI. It was too new for commercial work in early 2020, and later I was learning other languages.
Here are the resources I use to learn SwiftUI for work in 2021.
Apple
When SwiftUI just came out, its official documentation was notoriously poor. Over two years, the state of documentation (and SwiftUI itself) has significantly improved, and the macOS post I’ve linked above says:
Prioritize watching WWDC and reading official documentation, there is now a lot of outdated information about SwiftUI online.
The third-party information became outdated because SwiftUI moved fast and broke all the things. It was also the reason why I was reluctant to start learning it right away—I knew it would take a couple of years to settle down and not change from under you every month. (I’m not a big fan of the Hamster Wheel of Backwards Incompatibility either.)
Anyway.
The root of all things SwiftUI is, for some reason, under Xcode and not directly linked to from the bottom of the Apple Developer homepage.
From there, we have links to WWDC videos, reference documentation and (new!) tutorials.
Compared to the earlier Apple’s practice of dumping inconsistent source code examples on (often unsuspecting) developers, SwiftUI tutorials are well-structured, split into sequential chapters and cover just the questions a developer already familiar with iOS—but not SwiftUI—would have.
My preference is to first watch several of the earlier WWDC videos to get the basics (“Introducing SwiftUI,” “SwiftUI Essentials” and “Data Flow Through SwiftUI”), followed by those highlighting the latest developments (“What’s new in SwiftUI” and “App Essentials in SwiftUI”). After that, flick through the tutorials.
It helps that I have an app idea I want to make so that I can focus on specific SwiftUI features rather than just try and “learn” it.
Moving parts
Coming from “regular” Swift, SwiftUI is different in that it depends on three language features to work that may not be as frequent in other code. Here’s some sample SwiftUI code using all of them:
struct Sample: View {
@State private var value = 0
var body: some View {
VStack {
Text("Value equals \(value)")
Button("Increment") {
value += 1
}
}
}
}
- Property wrappers. Whenever you see
@State
,@Binding
or$projectedValue
, it’s a property wrapper. Swift documentation gives a good overview, and I agree with Mattt that “property wrappers further complicate Swift.” There’s a guide to all available SwiftUI property wrappers on Hacking with Swift. - Result builders (previously known as “function builders”). Listing several SwiftUI
View
s one after another separated only by newlines and no other syntax, for example in aVStack
like above, makes use of a result builder. I found this article useful to understand how they work. - Opaque types. The
some View
that is the type ofbody
above is an opaque type. Swift documentation goes into considerable detail on what they are and why you might need them.
The elephant in the room is, of course, the Combine framework that underpins bindings, but it seems for purely SwiftUI code we don’t need to know much about it. I’m sure I’ll dive into it separately, but it’s very similar to both RxSwift and ReactiveSwift that I’ve used for years, so I already know the concepts and how to think and write code over streams of values.
Awesome
There are several awesome
-style lists of links to SwiftUI articles and tutorials.
I asked a few friends who’ve been working with SwiftUI almost since its release, and they recommended The SwiftUI Lab and the SwiftUI articles on Swift with Majid.