【问题标题】:How to use model objects instead of arrays and dictionaries?如何使用模型对象而不是数组和字典?
【发布时间】:2011-09-08 14:54:51
【问题描述】:

下面是我如何从 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 模式为上述场景构建模型的正确方法?我猜我会使用单例来返回一组 Name 对象。我基本上想学习使用模型对象来表示我的数据的正确方式。

【问题讨论】:

  • @josh - 这里有两个不同的问题。 first question 是“为什么要使用模型,我不能只使用数组、字典吗?”。这是“好的,我确信,我该如何使用它们,这就是我尝试过的?”。由于这种区别,我很高兴让它们都保持开放,否则我们会遇到一个大问题,问两个不同的事情。

标签: iphone objective-c ios model-view-controller


【解决方案1】:

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

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

在将数据保存到 plist 文件之前,您需要将数据序列化回简单的数据结构。

NSMutableArray* arrayToStore = [Person dictionaryArrayFromPersonArray:people];

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

【讨论】:

  • 感谢您的意见。所以你建议我首先创建一个具有姓名、地​​址、电话号码等属性的 People 类。然后创建另一个具有这些转换功能的类?
  • 我建议将转换方法包含在 Person 类的类方法中。如果您想更好地将转换代码与模型代码解耦,那么像“PersonSerializer”这样的单独类是一个很好的解决方案。
【解决方案2】:

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

这类似于 UNIX 引擎接口。这不是 Smalltalk 的 MVC,其中 View 直接通过 MODEL(观察者模式)的更改来更新。

【讨论】:

  • 你能告诉我 +(id)modelGetInstance 方法的实现是什么样的吗?
  • @iPhoneDeveloper 我正在“重新学习”Objective C,因此我添加了该方法作为便利构造的练习。通常我会这样做:model= [[Model alloc]init];并在 dealloc 中向模型发送释放消息,或者我相信您可以向便利构造函数发送参数消息。我写了一个没有参数的便利构造函数: +(id)modelGetInstance { //id temp= [[self alloc] init]; //返回[临时自动释放]; return [[[self alloc] init] autorelease]; } 作为练习。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 2014-07-26
  • 2011-10-02
相关资源
最近更新 更多