Junior — Middle
What approach is used to execute commands in your system?
sobes.tech AI
Answer from AI
To execute commands in iOS, the Command pattern or delegation through closures is often used. In MVVM, for example, the ViewModel can provide commands as methods or closures, which the View calls upon user interaction.
Another common approach is using the Command pattern, where a command encapsulates an action and can be executed, undone, or repeated.
An example using closures:
class ViewModel {
var onButtonTap: (() -> Void)?
func buttonTapped() {
onButtonTap?()
}
}
// In ViewController
viewModel.onButtonTap = {
print("Command executed")
}