Question

I made an app but I want to refactor it to follow a known design pattern (MVC). Can someone explain what code should be in what class

So now I have these classes:

ViewController
-- properties
name
age
-- methods

DetailViewController
-- properties
detail array
-- methods

FavoritesViewController (tableview)
-- properties
favorites array
-- methods

AllNamesViewController (tableview)
-- properties
name
age
-- methods

The app works like this:

User opens the App -He can click question mark and gets a name and other properties back in json format. That opens the DetailViewController and pushes the name and age into the DetailViewController's detail array.

Favorites -He can go back to start screen and swipe to left for FavoritesViewController and see the names he saved...when he clicks it, it goes back to the DetailsViewController and push the favorites array into the detail array.

AllNames -He can swipe to right for all names that has all the names in a array when he clicks one it goes to the DetailViewController and pushes the array element into the array of DetailViewController.

What kind of object should be a datamodel? and how would that data model look like?

The part of json method should that always be in the controller or in the data model?

How can i determine what should be a data model. I read a lot of books and followed the tutorials but I can never see when something should be in a data model or not.

So can someone clarify it with the app I made so that maybe I get that "OOOHH" idea :)

Was it helpful?

Solution

According to the MVC pattern, you have three separate components: Model, View, Controller.

In iOS, a viewController plays the role of the Controller in the MVC pattern - it is meant to handle the communication between your view and the data.

The viewController's view property plays the role of View in the MVC pattern - this is the UI to be displayed.

How you implement the Model portion is up to you, but from what I understand from your question it looks like you have two responsibilities - a data object that holds your information, and storage of that data (in JSON).

I would create two classes for this, one that is just a data object (called Person or something, not sure what your use case is) that has properties for whatever data you need to store, and very few (if any) actual methods. The second would be some kind of storage class where you pass in Persons to be stored, and query them back (i.e. getAll, getFavorites, etc...). This class would handle conversion to and from JSON, and its interface would deal exclusively with Person objects.

Your controllers would then query this storage class, retrieve an array of Persons, and display them somehow (in a table it looks like).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top