A Long Weekend With SwiftUI

A Long Weekend With SwiftUI

Tutorials apple ios-development ios13 swiftui xcode

A Long Weekend With SwiftUI

So I spent most of the Fourth of July weekend not grilling anything or watching fireworks, but sitting in Xcode fighting with SwiftUI previews that kept crashing on me. Worth it? Mostly, yeah. Let me explain why, and also why I still wouldn't ship anything real with it yet.

Quick context in case you missed WWDC last month: Apple dropped SwiftUI as this brand new declarative UI framework, a total departure from the UIKit-and-storyboards way of building iOS apps that's been around since basically forever. Instead of dragging views onto a canvas and wiring up Auto Layout constraints until you want to throw your laptop out a window, you just describe what the UI should look like in code, and the framework figures out how to render it. You write something like VStack { Text("Hello") } and it stacks things vertically without you touching a single constraint. I know that sounds small. It is not small once you've spent a decade fighting NSLayoutConstraint.

I've had the betas installed on a spare test device since the first one, but I hadn't actually sat down and built anything with SwiftUI until this weekend. Xcode 11 beta plus iOS 13 on the simulator, and honestly the live preview canvas is the part that sold me fastest — you edit code on the left, and a rendered preview updates on the right in something close to real time. When it works it's kind of magic. When it doesn't, which was often for me on Saturday night, the canvas just sits there spinning or throws a vague "Cannot preview in this file" error with zero useful information. I probably restarted Xcode nine or ten times. That's not an exaggeration, I actually lost count around eight.

The @State property wrapper is the other thing that took some getting used to. Coming from UIKit where you're manually calling setNeedsLayout or manually updating labels after some action fires, SwiftUI wants you to just mark a variable as @State and then reference it in your view. Change the variable, the view redraws itself automatically. It's the same idea as React or Vue if you've touched either of those on the web side, which I have a little, mostly out of curiosity a couple years back. If you're a longtime iOS dev with zero web background this reactive-UI stuff is going to feel foreign for a bit. Give it a weekend and it clicks.

Combine, Apple's other new framework this cycle, is the part I bounced off of hardest. It's their reactive programming framework, kind of their answer to RxSwift, and it's supposed to pair with SwiftUI for handling async data and events. I got a basic example working where a network-ish mock publisher updated a Text view, but the moment I tried anything more complicated I ended up staring at error messages about generic type constraints that made me feel like I was reading a legal document. I'll come back to it. Not this weekend though, my patience had limits.

One thing I'll say clearly, not hedging on this one: don't build anything you plan to ship with SwiftUI right now. It only works on iOS 13, which doesn't even have a real release date yet beyond "this fall," meaning your app would be dead on arrival for anyone running iOS 12 or earlier, which as of today is still almost everybody. This is very much a "learn it on the side, ship it later" situation for anyone with actual users. Fine for weekend tinkering, bad idea for your day job codebase in July 2019.

Small tangent because I can't not mention it: half my Twitter timeline this weekend was footage from the Ridgecrest earthquakes out in Southern California, that 6.4 on the Fourth followed by an even bigger one the next day. Wild seeing parking lot footage of the ground actually rolling like water. The other half of my timeline was people complaining about SwiftUI previews crashing, which felt like a strange split-screen weekend for tech Twitter, natural disaster on one side and framework beta gripes on the other.

Anyway. If you've got some idle time and an Apple Developer account, I'd say pull down the latest beta and build something dumb with SwiftUI, a little counter app or a list of your favorite pizza toppings, whatever. It's genuinely a nicer way to write UI code than storyboards ever were, even half-baked as it currently is. I'll probably post some actual code snippets once I've got something less embarrassing than my current test project, which right now is just three buttons and a label that occasionally forgets how to preview itself.