• 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 archiver
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Keyed archiver


  • Subject: Re: Keyed archiver
  • From: Jonathan Jackel <email@hidden>
  • Date: Sun, 21 Nov 2004 07:00:48 -0500


On Nov 21, 2004, at 4:47 AM, Gerriet M. Denkmann wrote:

I have an object, which I want to archive.

- (void)encodeWithCoder:(NSCoder *)coder
{
	[ coder encodeObject: myString  forKey: @"myString" ];
}

- (id)initWithCoder:(NSCoder *)decoder
{
	myString = [ decoder decodeObjectForKey: @"myString" ];
	NSLog("@ myString: \"%@\");
	return self ;
}

When myString is @"$noll" everything works just fine.
But if myString is @"$null" (note the changed vowel) the unarchive string is nil.

Four things:

1. Always call super in your init method.  Always.

2. Your NSLog statement looks wrong. There's no parameter and no closing quote mark (the escape sequence at the end doesn't count). And the first @ should go outside the first quote mark.

3. Use accessors to set ivars. Otherwise your ivars will be deallocated prematurely. This could be your problem.

4. I think it is a good practice to use constant strings, defined outside the implementation, as keys. That way you will get a compiler warning if you type them incorrectly. For example:

NSString *MYStringKey = @"MYStringKey";

@implementation MyClass

***

- (void)encodeWithCoder:(NSCoder *)coder
{
	[ coder encodeObject:[self myString]  forKey: MYStringKey ];
}

- (id)initWithCoder:(NSCoder *)decoder
{
	if(self = [super init])
	{
		[self setMyString:[ decoder decodeObjectForKey: MYStringKey ]];
		NSLog(@"myString = %@", myString);
	}
	return self ;
}

Try it that way and see if you still have problems.

Jonathan

_______________________________________________
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 archiver
      • From: "Gerriet M. Denkmann" <email@hidden>
References: 
 >Keyed archiver (From: "Gerriet M. Denkmann" <email@hidden>)

  • Prev by Date: Re: cocoa java bridge: converting nsarray to java array
  • Next by Date: Re: Keyed archiver
  • Previous by thread: Keyed archiver
  • Next by thread: Re: Keyed archiver
  • Index(es):
    • Date
    • Thread