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
ObservableObjectprotocol can use SwiftUI’s
@Publishedproperty wrapper to automatically announce changes to properties, so that any views using the object get their
bodyproperty 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 called
Every class that conforms toHacking with Swift, Paul Hudson (@twostraws)
ObservableObjectautomatically gains a property called
objectWillChange. This is a publisher, which means it does the same job as the
@Publishedproperty 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
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 SwiftUIHacking with Swift, Paul Hudson (@twostraws)
Imageview 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.
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!