Michael Grant

Michael Grant

SwiftUI class notes 2022-01-29

Screen Shot 2022-01-29 at 17.24.04.png
For now at least, weekends (and holidays) are the only times I really have the opportunity to focus on code for more than an hour or two at a time, so if I want to complete the more difficult “challenges” in 100 Days of SwiftUI or other courses, that’s when I’ll have to work on them. That means setting aside work on my own app(s) any time I reach a course challenge that I can’t get past in a couple of hours. Since (at least at my skill level, and maybe at all) there’s no reliable way of estimating how long it will take to reach a stopping point in my independent work, I can’t really do it the other way around. So I’m setting aside MinderBoard and turning back to 100 Days of Swift, which I’d like to finally finish within the next few weeks.

I’m actually focused on UIViewControllerRepresentable right now in both MinderBoard and the 100 Days challenge anyway.

Nick Sarno’s Use UIViewControllerRepresentable to convert UIKit controllers to SwiftUI video on YouTube and Paul Hudson’s series starting with Wrapping a UIViewController in a SwiftUI view cover very similar territory. Without digging through Apple’s documentation, the UIImagePickerController type in Nick’s video and the PHPickerViewController in Paul’s seem to do (exactly?) the same thing.

The code in the two tutorials is also very similar, with a few notable exceptions. Rather than replicating the view controller struct’s @Binding variables inside the Coordinator class, Paul creates a reference to the view controller as the Coordinator’s parent, allowing us to access its properties. This seems like a much more elegant approach. Using the view controller’s .dismiss() method also feels cleaner than passing showScreen around as a binding.

By defining the @State image variable in his main SwiftUI ContentView as an (optional) UIImage? rather than an (optional) SwiftUI Image? in the first place, on the other hand, Nick has no need of Paul’s additional @State inputImage variable, loadImage() function, and .onChange() modifier.

Paul’s beautiful dogs make an appearance in his videos, so that’s points for him. Advantage, Paul Hudson.


For Paul’s Challenge I do need to detect when a new photo is added, so I’ll actually still need the .onChange modifier. But it’s getting to be dinner time, so I’ll continue tomorrow.