How to use model objects instead of arrays and dic

2019-07-28 23:23发布

问题:

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.

回答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).



回答2:

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.