Daily iOS
SwiftUI

MVVM in SwiftUI

If you apply MVVM to a screen in a SwiftUI app that supports iOS 17 and later, how do you define the ViewModel and how does the view own it?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Put the screen state and action methods in a @MainActor @Observable final class ViewModel, have the view own it as @State private var vm, and inject dependencies through init.

Share a question