Skip navigation.
 
mlRe: Keyed archiver
FROM : Jonathan Jackel
DATE : Sun Nov 21 13:00:48 2004

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

Related mailsAuthorDate
mlKeyed archiver Gerriet M. Denkman… Nov 21, 10:47
mlRe: Keyed archiver Jonathan Jackel Nov 21, 13:00
mlRe: Keyed archiver Gerriet M. Denkman… Nov 21, 13:53
mlRe: Keyed archiver Joakim Danielson Nov 21, 15:09
mlRe: Keyed archiver Gerriet M. Denkman… Nov 21, 19:31
mlRe: Keyed archiver Joakim Danielson Nov 21, 20:15