SpokaneDude SpokaneDude - 1 year ago 68
Objective-C Question

How to persist objects between classes

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;


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 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 have looked through Google, but found nothing that answers my specific question.

Answer Source

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

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"];

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download