Re: Property List Question
Re: Property List Question
- Subject: Re: Property List Question
- From: Stefan Arentz <email@hidden>
- Date: Thu, 17 May 2001 10:56:05 +0200
On Thursday, May 17, 2001, at 12:35 AM, Karl Goiser wrote:
...
>
You know, all they need to do is have a protocol which requires your
>
objects to be translatable between NSArray, NSString or NSDictionary
>
objects and everybody could take advantage of the inherent property
>
list methods!
>
>
Ah well, back to the grind.
That's why we have NSCoding. Here is a simple example:
@interface CFilter : NSObject <NSCoding>
{
NSString *mFilter;
NSString *mDescription;
}
- (id) init;
- (void) dealloc;
- (void) setFilter: (NSString*) aFilter;
- (NSString*) getFilter;
- (void) setDescription: (NSString*) aDescription;
- (NSString*) getDescription;
@end
@implementation CFilter
- (id) init
{
if (self = [super init])
{
mFilter = mDescription = @"";
}
return self;
}
- (void) dealloc
{
[self setFilter: nil];
[self setDescription: nil];
}
- (void) setFilter: (NSString*) aFilter
{
if (aFilter != mFilter) {
[mFilter release];
mFilter = [aFilter retain];
}
}
- (NSString*) getFilter
{
return mFilter;
}
- (void) setDescription: (NSString*) aDescription
{
if (aDescription != mDescription) {
[mDescription release];
mDescription = [aDescription retain];
}
}
- (NSString*) getDescription
{
return mDescription;
}
- (void) encodeWithCoder: (NSCoder*) coder
{
[coder encodeObject: mFilter];
[coder encodeObject: mDescription];
}
- (id) initWithCoder: (NSCoder*) coder
{
if (self = [super init])
{
mFilter = [[coder decodeObject] retain];
mDescription = [[coder decodeObject] retain];
}
return self;
}
@end
Ok, now say you have an NSArray of CFilter object and you want to write
it to a file:
NSArray *filters;
[NSArchiver archiveRootObject: filters toFile: @"/path/to/file"];
Unarchiving is left as an excercise for the reader ;)
Stefan