Iphone 如何使用模型对象而不是数组和字典?

Iphone 如何使用模型对象而不是数组和字典?,iphone,objective-c,ios,model-view-controller,Iphone,Objective C,Ios,Model View Controller,下面是我如何从plist读取数据并在表视图中显示数据的简单示例。如果我用一个对象来表示我的模型,我会怎么做呢 @interface RootViewController : UITableViewController { NSMutableArray *namesArray; } @property (nonatomic, retain) NSMutableArray *namesArray; @end @implementation RootViewController @synt

下面是我如何从plist读取数据并在表视图中显示数据的简单示例。如果我用一个对象来表示我的模型,我会怎么做呢

@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;
}

有人能告诉我在上述场景中,按照MVC模式构建模型的正确方法吗?我猜我将使用单例返回一组名称对象。我主要想学习使用模型对象表示数据的正确方式

如果您想使用plists进行存储,并使用更像MVC的模型进行编程,则需要编写从该模型到简单数据结构的转换函数,反之亦然。例如,如果您有个人模型:

NSMutableArray* tempArray = [[NSMutableArray alloc]initWithContentsOfFile:path];
NSMutableArray* people = [Person personArrayFromDictionaryArray:tempArray];
在将数据保存到plist文件之前,需要将数据序列化回简单的数据结构

NSMutableArray* arrayToStore = [Person dictionaryArrayFromPersonArray:people];

如果您只需要跟踪名称列表,最好在整个程序中将它们表示为字符串数组。

iOS MVC中的模型只是将您的应用程序分割开来,以便将数据和应用程序算法(模型)与表示代码和事件处理代码分开。因此,考虑创建一个新的模型类,它获取、设置和持久地保存应用程序数据。这个类应该不了解GUI

下面是一个模型示例,该模型通过处理加密和解密来包装应用程序算法

#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
#导入
#进口
#进口
#进口
@接口模型:NSObject{
}
+(id)modelGetInstance;
+(NSData*)getRandomIV:(NSInteger)numBytes;
-(id)init;
-(NSString*)加密:(NSString*)明文密码:(NSString*)pw;//公众的
-(NSString*)解密:(NSString*)密文密码:(NSString*)pw;//公众的
@结束

这类似于UNIX引擎接口。这不是Smalltalk的MVC,在该MVC中,视图通过模型(观察者模式)的更改直接更新。

可能与@josh重复-这里有两个不同的问题。问题是“为什么要使用模型,我不能只使用数组、字典吗?”。这是“好的,我确信,我该如何使用它们,这是我尝试过的?”。我很高兴让他们都开放,因为他们的区别,否则我们会有一个大问题问两个不同的事情。谢谢你的投入。因此,您建议我首先创建一个具有名称、地址、电话号码等属性的People类,然后创建另一个具有这些转换函数的类?我建议将转换方法作为Person类的类方法。如果您希望更大程度地将转换代码与模型代码分离,那么像“PersonSerializer”这样的单独类是一个很好的解决方案。您能告诉我+(id)modelGetInstance方法的实现是什么样子吗?@iPhoneDeveloper我正在“重新学习”目标C,因此我添加了该方法作为一个方便构造的练习。通常我会这样做:model=[[model alloc]init];并在dealloc中发送模型发布消息,或者我相信您可以向方便的构造函数发送带有参数的消息。作为练习,我编写了一个没有参数的方便构造函数:+(id)modelGetInstance{//id temp=[[self alloc]init];//return[temp autorelease];return[[self alloc]init]autorelease];}。