Steven B.
Steven B.

Reputation: 45

MVC - Accessing model data (Objective C)

I am currently learning Objective-C, one step at a time! Right now, my main focus is the MVC pattern. I'm not sure if I've wrapped around the concept very well yet.

So, a little bit of background info: I am currently experimenting with "entities" in my program. The model for the entities holds information such as health and speed. I've already implemented a very basic controller that can take advantage of this entity. The input is taken through a command line interface, which is basically my view.

Now, my question is: how should my entity controller access the information from the model property? I need to be able to have an array of the "entities" that the entity controller handles.

In my controller's @interface: I have declared:

NSMutableArray *entities; 

Then in my @implementation for the controller, I override the init function. In this function:

entities = [[NSMutableArray alloc] init]; 

I also have another function that "spawns" a new entity.

- (void)spawnEntity {

Entity *entity = [[Entity alloc] init];
[entity setName:@"Default"];
...
...
[entities addObject: entity];
}

Then from main (which will later be a true view controller. Once I actually set up the app on the iPhone, as now I am just preparing the models and some of the controllers.):

EntityController *entityController = [[EntityController alloc] init];

[entityController spawnEntity];

Is this approach the correct way to utilize the MVC pattern? My main concern is the NSMutableArray I have declared in the controller. I am not sure if I am doing this properly.

Any feedback would be greatly appreciated.

Thank you.

-Steven

Upvotes: 2

Views: 726

Answers (3)

UmbalaAZ
UmbalaAZ

Reputation: 165

I have implemented Repository pattern for data access layer in iOS app, this can be used with CoreData entity framework, or REST API. Please check out at https://github.com/ducngo-tpl/Repository-Pattern-in-Objective-C. Hope it help.

Upvotes: 0

Tim Vermeulen
Tim Vermeulen

Reputation: 12562

You merged the Model and the Controller of your MVC design pattern in your EntityController. Sometimes, doing that can be the perfect fit, but there are some reasons you would want them apart from each other. A reason to split them would be to have easier to understand code as you add classes, methods and variables. Another reason would be reusability of your code.

In your example, this could be possible by creating a new class for your model object which has an NSMutableArray instance variable. Your controller then could create an instance of that class (or a singleton) to add new entities to, rather than to its own array.

If the array is the only variable you need for this application, though, you could perfectly stick with your current pattern. An NSArray (or one of its subclasses) is a model object on its own, so it would be unnecessary to wrap it in another class.

For more info, see the Apple Docs.

Upvotes: 1

Michael Brown
Michael Brown

Reputation: 498

MVC is a pattern to provide Seperation of Concerns. The "Model" and "View" are collections/groupings of relating objects. Each object has complete functionality and should conform to OO principles.

The Model objects will relate to your business concepts, e.g. with types User, Account, Address etc.

The View objects will normally be of type Button, Textarea etc.

The controller provides an interface for calling interactions between your Model objects, which is your business logic.

Traditionally, View objects can communicate directly with Model objects to gather display information.

Upvotes: 0

Related Questions