Re: Keyed encoding
Re: Keyed encoding
- Subject: Re: Keyed encoding
- From: Will Mason <email@hidden>
- Date: Tue, 21 Jun 2005 10:13:52 -0700 (PDT)
Your code looks good, except that you're using keyed-archiving methods
with a non-keyed archiver. I would use NSKeyedArchiver instead of
NSArchiver. I'm pretty sure that's the problem.
Take care,
Will
--- Andy Bettis <email@hidden> wrote:
> On 21 Jun 2005, at 17:43, Will Mason wrote:
>
> >> The arrays contain custom class objects. For each of these I have
> >> defined initWithCoder methods that use encodeObject:forKey,
> >> encodeBool:forKey, etc. calls to archive their data members. The
> >> calls to encodeObject:forKey only pass built-in class objects
> >> (NSString, NSNumber, NSCalendarDate).
> >
> > initWithCoder: is for decoding, not for encoding, so calling
> > encodeObject:forKey: in initWithCoder: is not very productive. Have
>
> > you
> > defined encodeWithCoder: for your custom classes, putting the
> > encodeObject:forKey: in that method?
>
> Oops, naughty fingers not typing what I intended! My classes have
> methods like this:
>
> - (void) encodeWithCoder:(NSCoder*) inCoder
> {
> [super encodeWithCoder:inCoder];
>
> [inCoder encodeBool:mIncome forKey:kIncomeLabel];
> [inCoder encodeBool:mExpenditure forKey:kExpenditureLabel];
> }
>
> - (id) initWithCoder:(NSCoder*) inCoder
> {
> if (self = [super initWithCoder:inCoder]) {
> [self setIncome:[inCoder decodeBoolForKey:kIncomeLabel]];
> [self setExpenditure:[inCoder
> decodeBoolForKey:kExpenditureLabel]];
> }
>
> return(self);
> }
>
> While my document class has:
>
> - (NSData *)dataRepresentationOfType:(NSString *)aType
> {
> NSMutableDictionary* theDict = [NSMutableDictionary dictionary];
> [theDict setValue:[self categories] forKey:kCategoriesLabel];
> [theDict setValue:[self items] forKey:kItemsLabel];
>
> return([NSArchiver archivedDataWithRootObject:theDict]);
> }
>
> - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString
> *)aType
> {
> NSDictionary* theDict = [NSUnarchiver
> unarchiveObjectWithData:data];
>
> [mCategories release];
> mCategories = [theDict valueForKey:kCategoriesLabel];
> [mCategories retain];
>
> [mItems release];
> mItems = [theDict valueForKey:kItemsLabel];
> [mItems retain];
>
> return(YES);
> }
>
>
> Rev. Andy
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden