From the course: iOS Development: Architecture

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Refactoring to MVP: Model

Refactoring to MVP: Model

From the course: iOS Development: Architecture

Start my 1-month free trial

Refactoring to MVP: Model

- [Narrator] Instead of accessing the model directly, we extract it to a dedicated model type. I create a new Swift file called Model. Now, I declare a structure called Model. Let's make it generic so that it can store any type. We need a backend storage. The private objects property is a generic array. Next, we need a method for inserting new elements. The insert method is mutating since it modifies the contents of the struct. The method simply inserts the new element to the beginning of the list. We also have to remove elements from the model, thus I provide the remove at index method. It's also mutating since it modifies the contents of the struct. It is recommended to check whether the index is valid. Calling the arrays Remove At method with an invalid index would crash the app, so I only call Remove At if the index passed its argument can be found within the array's instances. The model should provide the number of its entries. The count read only property returns the number of…

Contents