onAppear()
?Suggested approach: Using init()
and onAppear()
both let us run some code early in a view's lifecycle, however it's important to understand the difference between them.
SwiftUI creates all its view structs immediately, even creating destination views for navigation links, which means that initializers are run immediately and that's probably not something you want. In comparison, code placed in an onAppear()
modifier is called only when the view is shown for the first time, so it's the right place to do complex work.
Estimated difficulty: Easy
See the full list of iOS interview questions
Link copied to your pasteboard.