Component - MVVM pattern
The MVVM pattern helps organize app code by separating the user interface (View), the data and business logic (Model), and the connection between them (ViewModel). This makes apps easier to build and maintain.
The MVVM pattern helps organize app code by separating the user interface (View), the data and business logic (Model), and the connection between them (ViewModel). This makes apps easier to build and maintain.
ViewController
├── View (UI elements)
└── ViewModel
└── Model (Data)class CounterViewModel: ObservableObject {
@Published var count = 0
func increment() { count += 1 }
}
struct ContentView: View {
@ObservedObject var viewModel = CounterViewModel()
var body: some View {
VStack {
Text("Count: \(viewModel.count)")
Button("Tap me") {
viewModel.increment()
}
}
}
}class UserViewModel {
@Published var username: String = ""
func updateName(newName: String) {
username = newName
}
}Pass the test and this pattern moves unknown