Junior — Middle
How does data exchange and interaction between system layers occur when creating a new application?
sobes.tech AI
Answer from AI
In iOS applications, interaction between layers is usually built according to architectural patterns (MVC, MVVM, VIPER, etc.).
Data exchange occurs through:
- Models: store data and business logic.
- Views: display data to the user.
- Controllers or ViewModels: manage logic and connect the model with the view.
Data is transmitted via methods, delegates, closures, or reactive libraries (e.g., Combine, RxSwift).
Example: The ViewModel receives data from the model and updates the View through binding or callback.
class ViewModel {
var dataUpdated: (() -> Void)?
private var data: [String] = [] {
didSet {
dataUpdated?()
}
}
func fetchData() {
// Get data from the model
data = ["Item1", "Item2"]
}
}
class ViewController: UIViewController {
let viewModel = ViewModel()
override func viewDidLoad() {
super.viewDidLoad()
viewModel.dataUpdated = { [weak self] in
// Update UI
self?.tableView.reloadData()
}
viewModel.fetchData()
}
}