如何在类之间持久化对象 [英] How to persist objects between classes

查看:100
本文介绍了如何在类之间持久化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有6个类别,其中包含唯一数据;我定义了一个看起来像这样的类:

I have 6 categories that contain unique data; I have defined a class that looks like this:

@interface ExportBookData : NSObject {

}

@property (strong, nonatomic) NSArray *booksArray;
@property (nonatomic, retain) NSMutableDictionary *builtFileList;
@property (nonatomic, retain) NSMutableArray *exportData;

@end

我想做的是能够实例化类ExportBookData,每个类别一次,并在另一个类中使用该实例化的类,使数据持久存在并且可在类之间访问。

What I want to do is be able to instantiate the class ExportBookData, once for each category, and use that instantiated class throughout another class, having the data persist and be accessible between classes.

我已经尝试过此代码,但是不能满足我的需要:

I have tried this code, but it doesn't do what I need:

ExportBookData *abe = [ExportBookData new];  
abe.abeBuiltFileList = [NSMutableDictionary dictionary];
abe.abeExportData = [NSMutableArray arrayWithCapacity:abe.abeBooksArray.count];

UPDATE 的对象;我有名为安倍,球,比较,呼叫者,小屋和房屋的类别。我希望该类具有可以被称为Abe,Balls等的属性。我不知道该如何使用定义的属性来实现。

UPDATE The problem is in the addressing of the objects; I have categories named Abe, Balls, Comp, Caller, Hut, and House. I want the class to have properties that can be addressed as Abe, Balls, etc. I can't figure out how to do that with what I have defined.

I曾经浏览过Google,但没有找到能回答我特定问题的东西。

I have looked through Google, but found nothing that answers my specific question.

推荐答案

封装,封装,封装!将特殊知识放在类本身内。

Encapsulate, encapsulate, encapsulate! Put the special knowledge inside the class itself.

假设您有一个ExportBookData对象,该对象的行为取决于所使用的书商。然后提供一个采用书商类型的初始化程序:

Let's say you have an ExportBookData object that behaves differently depending which bookseller it uses. Then provide an initializer that takes a bookseller type:

ExportBookData *abe = [[ExportBookData alloc] initWithCategory:@"Abe"];

好的,所以现在Exportem的这个实例知道它的行为应为安倍型行为。但是,无论如何初始化ExportBookData,其公共属性名称都将相同,例如 builtFileList exportData ,这样您就可以引用 abe.builtFileList ,这将是安倍晋三的 right 列表。

Okay, so now this instance of ExportBookData knows that its behavior should be Abe-type behavior. But no matter how an ExportBookData is initialized, its public property names will all be the same, e.g. builtFileList and exportData, so you'll then be able to refer to abe.builtFileList and this will be the right kind of list for an Abe.

这篇关于如何在类之间持久化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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