How do I separate view, view model, and model layers?

In this example, we will explore how to separate the view, view model, and model layers in a Swift application using the MVVM architecture.
Swift, MVVM, View Model, View, Model, Layer Separation
// Model struct User { var name: String var age: Int } // ViewModel class UserViewModel { private var user: User var userName: String { return user.name } var userAge: String { return "\(user.age) years old" } init(user: User) { self.user = user } } // View (Example User Interface) class UserView { var viewModel: UserViewModel init(viewModel: UserViewModel) { self.viewModel = viewModel } func displayUserInfo() { print("Name: \(viewModel.userName), Age: \(viewModel.userAge)") } } // Usage Example let user = User(name: "John Doe", age: 30) let viewModel = UserViewModel(user: user) let userView = UserView(viewModel: viewModel) userView.displayUserInfo() // Output: Name: John Doe, Age: 30 years old

Swift MVVM View Model View Model Layer Separation