1

Below is a simple example of how I'm reading from a plist and displaying the data in a table view. If I were to use a objects to represent my model, how would I be doing that?

@interface RootViewController : UITableViewController {
    NSMutableArray *namesArray;
}
@property (nonatomic, retain) NSMutableArray *namesArray;
@end


@implementation RootViewController
@synthesize namesArray;

- (void)viewDidLoad{
    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"names" ofType:@"plist"];

    NSMutableArray *tempArray = [[NSMutableArray alloc]initWithContentsOfFile:path];

    self.namesArray = tempArray;

    [tempArray release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [namesArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [namesArray objectAtIndex:indexPath.row];
    return cell;
}

Can anyone show me the proper way of constructing my Model as per MVC pattern for the above scenario? I'm guessing I would be using a singleton to return a set of Name objects. I basically want to learn the correct manner of using Model objects to represent my data.

NSExplorer
  • 11,849
  • 12
  • 49
  • 62
  • possible duplicate of [Model classes. What the heck do they really mean?](http://stackoverflow.com/questions/6240860/model-classes-what-the-heck-do-they-really-mean) – jscs Jun 05 '11 at 04:53
  • @josh - there's two different questions here. The [first question](http://stackoverflow.com/questions/6240860/model-classes-what-the-heck-do-they-really-mean) is a "Why use models, can't I just use Arrays, Dictionaries?". This is the "OK, I'm convinced, how do I use them, here's what I tried?". I'm happy to keep them both open because of that distinction, otherwise we get a big question asking two different things. – Kev Jun 05 '11 at 12:40

2 Answers2

1

If you want to use plists for storage and a more MVC like model for programming, you'll need to write conversion functions from that model to simple data structures and vice-versa. For example, if you had a Person model:

NSMutableArray* tempArray = [[NSMutableArray alloc]initWithContentsOfFile:path];
NSMutableArray* people = [Person personArrayFromDictionaryArray:tempArray];

Before saving the data to a plist file you would then need to serialize the data back to simple data structures.

NSMutableArray* arrayToStore = [Person dictionaryArrayFromPersonArray:people];

If you just need to keep track of a list of names, it might be best to represent them as an array of strings throughout the program.

jfocht
  • 1,704
  • 1
  • 12
  • 16
  • Thanks for your input. So you are suggesting that I first create a People class with attributes like name, addresses, phone numbers etc. Then create another class that has these conversion functions ? – NSExplorer Jun 05 '11 at 05:01
  • I was suggesting including the conversion methods as class methods of the Person class. If you want greater decoupling of the conversion code from the model code then a separate class like "PersonSerializer" is a fine solution. – jfocht Jun 05 '11 at 13:37
1

The Model in iOS MVC simply divides up your application so that the data and application algorithms (Model) are separated from the presentation and event handling code. So consider creating a new Model class that gets, sets and persist your application data. This class should have no knowledge of the GUI.

Here is an example of a model that wraps the application algorithms by handling encryption decryption.

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCryptor.h>
#import <CommonCrypto/CommonDigest.h>
#import <Security/Security.h>


@interface Model : NSObject {
}
+(id)modelGetInstance;
+(NSData *)getRandomIV:(NSInteger)numBytes;
-(id)init;
-(NSString*)encrypt:(NSString*)plainText password:(NSString*)pw; // public
-(NSString*)decrypt:(NSString*)cipherText password:(NSString*)pw;  // public
@end

This is similar to UNIX ENGINE-INTERFACE. This is NOT the MVC of Smalltalk in which the View is directly updated by changes in the MODEL (Observer pattern).

JAL
  • 3,319
  • 2
  • 20
  • 17
  • Could you show me what the implementation of +(id)modelGetInstance method would look like? – NSExplorer Jun 05 '11 at 06:15
  • @iPhoneDeveloper I am "re-learning" Objective C and so I added that method as an exercise in convenience construction. Normally I would do: model= [[Model alloc]init]; and send model a release message in dealloc or I believe you can message the convenience constructor with parameters. I wrote a convenience constructor with no parameters as: +(id)modelGetInstance { //id temp= [[self alloc] init]; //return [temp autorelease]; return [[[self alloc] init] autorelease]; } as an exercise. – JAL Jun 05 '11 at 06:28