• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Keyed encoding
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Keyed encoding


  • Subject: Re: Keyed encoding
  • From: Andy Bettis <email@hidden>
  • Date: Tue, 21 Jun 2005 17:53:58 +0100

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

  • Follow-Ups:
    • Re: Keyed encoding
      • From: Will Mason <email@hidden>
References: 
 >Re: Keyed encoding (From: Will Mason <email@hidden>)

  • Prev by Date: Re: NSTreeController - please help, can't see the forest for the tree controller
  • Next by Date: Correct way to force kill a thread.
  • Previous by thread: Re: Keyed encoding
  • Next by thread: Re: Keyed encoding
  • Index(es):
    • Date
    • Thread