We’ve arrived at day 80 of the 100 Days of SwiftUI. After today, we enter the final stretch of this course we’ll have completed a whopping 80% of the course. It’s been quite a journey and a difficult one at that, but we know what we’re doing it for.
Yesterday, we started on our new “Hot Prospects” app. We’ll continue this for the coming days. Let’s dive in!
Manually publishing ObservableObject changes in SwiftUI
Up until now, we’ve used SwiftUI’s @Published
property to automatically announce any changes to our properties. However, if we want some more, fine grain control, that’s possible as well.
Classes that conform to the
ObservableObject
protocol can use SwiftUI’s@Published
property wrapper to automatically announce changes to properties, so that any views using the object get theirbody
property reinvoked and stay in sync with their data. That works really well a lot of the time, but sometimes you want a little more control and SwiftUI’s solution is calledobjectWillChange
.Every class that conforms to
Hacking with Swift, Paul Hudson (@twostraws)ObservableObject
automatically gains a property calledobjectWillChange
. This is a publisher, which means it does the same job as the@Published
property wrapper: it notifies any views that are observing that object that something important has changed. As its name implies, this publisher should be triggered immediately before we make our change, which allows SwiftUI to examine the state of our UI and prepare for animation changes.
Understanding Swift’s Result type
The Result
type is a type that contains either some value or an error. This is comparable to an optional, which either contains a value or is empty. Check out Paul’s article on how we use the Result
type and why they’re very useful.
Controlling image interpolation in SwiftUI
Image Interpolation happens when we stretch content larger than its original size. The performance hit we take is, especially on modern devices, not noticeable, due to the vast amounts of computing power they have. There’s a catch though, which Paul explains in his article on image interpolation.
What happens if you make a SwiftUI
Hacking with Swift, Paul Hudson (@twostraws)Image
view that stretches its content to be larger than its original size? By default, we get image interpolation, which is where iOS blends the pixels so smoothly you might not even realize they have been stretched at all. There’s a performance cost to this of course, but most of the time it’s not worth worrying about.
Wrap up
That’s it for day 80! As we arrive at the final stretch of the course, keep yourself energised and keep going at it. I recommend looping over large parts of the course after you’ve finished to further solidify your knowledge and keep practicing. That’s my plan at least! For now, it’s time to recharge for tomorrow. Until then!
100 Days of SwiftUI – Day 80