Been thinking about what I've learned after working exclusively with SwiftUI for 6 months now (I really need to get a blog going) and thought I'd share some of them here 🧵
You need to throw everything you know about App/UIKit out for a while. SwiftUI is not only declarative in its syntax, but it’s also an abstraction where platform, SDK and context ultimately decides what will happen. Never take anything literally until you’ve tested it.
There are lots of implications to your apps behaviour and appearance happening under the hood all the time. One big culprit can be parameter defaults. Always check the function signatures in the header before using them.
A SwiftUI app consists of hundreds or thousands of views that can refresh and recreate constantly. Align your view and model design to limit the performance impact. Never do work in View inits, and compartmentalise subviews and their states. And always keep an eye on the CPU %.
SwiftUI didn't really become viable for production until version 2 (iOS 14/Big Sur). If you try to ship the older version, you will be spending more time fighting the framework than shipping features.
ObservableObject is awesome! But did you know that once you start subclassing it, all the functionality breaks down? stackoverflow.com/questions/5761…
Apple sample projects are not gospel. They are shallow showcases that’s meant to highlight certain features like widgets, app clips, platform interoperability and so on. The performance, UX, and sometimes even code practices should not be attributed to SwiftUI itself.
Very often your bugs will be originating from your data model. SwiftUI relies heavily on good data structures, and using it for real world apps with complex models and actions isn’t possible until you master Combine. Practical Combine by @DonnyWals is a great resource.
You will see conditional modifiers (applyIf) and AnyView used everywhere in tutorials and Stack Overflow, but they are huge code smells that come with big performance implications. Unless you really know how they work, don’t use them.
SwiftUI limitations are not actual limitations. Where the capabilities of SwiftUI ends, you are one short bridge away from letting App/UIKit handle the final stretch.
Lastly, don't trust the naysayers. I almost quit SwiftUI several times after seeing much more experienced devs dismiss it for being broken and incomplete. If someone doesn't have the credibility and experience with SwiftUI, chances are they don't know what they are talking about.
• • •
Missing some Tweet in this thread? You can try to
force a refresh