Hello, In this blog we are going to learn about the MVVM in swift
Model–View–ViewModel (MVVM) is a software architectural pattern that facilitates the separation of the development of the graphical user interface (the view)
The component of the MVVM
Model:
The model is used to store the data which can access from the view model
for example
1 2 3 4 5 6 7 8 |
struct ImageData { let title: String let imageType: String let imageURL: String init?(data: JSON) { } } |
View:
A view is used to show the user interface and content of the application to the user
You can use view controller, table view cell, collection view cell, uiview, etc
fo example:
1 2 3 4 5 6 7 8 9 10 11 12 |
class YourViewController: UIViewController { @IBOutlet weak var tableView: UITableView! @IBOutlet weak var searBtn: UIButton! @IBOutlet weak var searField: UISearchBar! var viewModel = YourViewModel() var searchText = "" override func viewDidLoad() { super.viewDidLoad() } } |
View Model:
In the View model, you can manage everything in the view controller
you can create an object for the model and put the view in the model from here
for example:
1 2 3 4 5 6 7 8 9 |
class SampleViewModal: NSObject { var sample = Sample() func assignValue(){ sample = Sample(data: Json(sampleData) } } |
you can easily manage the data and view it from here
You need to create an object in your view controller for the view model then you need to initialize the view model then you can use the view model from your view controller
Thank you for reading this article. I hope this article helped you to understand MVVM in swift
After that, if you want to read more articles regarding iOS Development click here, or if you want to read more about MVVM click here.
For other technical blogs, please click here.