The education of Paul Weichhart

Write like nobody is reading

High performance SwiftUI

After trying SwiftUI & looking through tutorials from Apple, Paul Hudson & others, I have to say I couldn’t be more excited. I was able to work with React during the last two years & seeing this library being sherlocked with first party support from Apple is truly amazing. SwiftUI is not only more expressive, easier to write and maintain, it also works seamlessly with Apple’s own Combine framework.

The case for React (Native)

Ash Furrow wrote some time ago why React (Native) is awesome — you shouldn’t use it because of the multi platform support, but because it handles state so elegant. Like Ash, I would advise everyone to read through the React Tutorial & especially through Thinking in React — it teaches the fundamentals on how to write reusable components and applications. Basically everything can & should be a component (or View in SwiftUI).

Applying everything to SwiftUI

By reading through various SwiftUI tutorial however, most demonstrated to put the whole UI into a single View. Knowing that React only re–renders components whose state or props (equivalent of properties in SwiftUI) changed, I was curious to see if SwiftUI handles changes the same way. Here is a playground Gist to provide a proof of concept.

Looking at the code it makes sense that Views themself are re–rendered if their state or properties changed, since they are represented as structs. But nothing stops us from extracting Subviews into distinct, reusable Views, that are only re–rendered in case of changes.

While it is more convenient & reduces the amount of boilerplate code to place every Subview into a single hierarchy, I wouldn’t advise to do so. While re–rendering isn’t expensive for Text, it could be for having a Map View for example.

The future is reactive

The concept of React and therefore SwiftUI is easy to understand & learn. While SwiftUI is still in Beta as of my writing, it already features a full set of tools & works really well. (I’ve updated the gist to work with Xcode 11.1). I’m really looking forward to use SwiftUI & Combine in the future.

Something to add or say? Drop me some characters on twitter @paulweichhart