如何使用模型对象而不是数组和字典? [英] How to use model objects instead of arrays and dictionaries?

查看:93
本文介绍了如何使用模型对象而不是数组和字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个简单的示例,说明了我如何从plist中读取数据并在表视图中显示数据.如果我要使用对象来表示我的模型,那我该怎么做?

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

对于上述情况,有人可以向我展示按照MVC模式构造模型的正确方法吗?我猜想我会使用单例来返回一组 Name 对象.我基本上想学习使用模型对象表示数据的正确方式.

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.

推荐答案

iOS MVC中的模型只是将您的应用程序进行划分,以便将数据和应用程序算法(模型)与表示和事件处理代码分开.因此,请考虑创建一个新的Model类,以获取,设置和持久化您的应用程序 data .此类不应了解GUI.

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

这类似于UNIX ENGINE-INTERFACE.这不是Smalltalk的MVC,在该MVC中,视图是通过MODEL(观察者模式)的更改直接更新的.

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

这篇关于如何使用模型对象而不是数组和字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆