Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSKeyedArchiver and NSKeyedUnarchiver with NSMutableArray

I'm hoping this isn't something to do with the fact that I'm using a Mutable array here, but this one is baffling me so it wouldn't surprise me if that were the case.

BACKGROUND:

I have made a small database which is essentially an NSMutableArray containing custom objects, which we can call recordObjects. I set up the array:

database = [[NSMutableArray alloc] init];

and my custom object, called "recordObject" contains the following variables and inits:

NSString *name;
int       anInt;
Bool      aBool;

I also synthesized methods so I can make calls like:

aString = [[database objectAtIndex:someIndex] name];

And added methods to my controller class to add, remove, and select the individual records for display. So far everything works correctly and exactly as expected.

Next, I've set up my recordObject class (subclass of NSObject) to use the NSCoder (by including in the @interface directive, and have added the following custom encoder and decoder methods in the implementation file:

-(void) encodeWithCoder: (NSCoder *) encoder {
    [encoder encodeObject: name  forKey: @"recordName"];
    [encoder encodeInt:    anInt forKey: @"recordInteger"];
    [encoder encodeBool:   aBool forKey: @"recordBool"];
}

-(id) initWithCoder: (NSCoder *) decoder {
    name    = [decoder decodeObjectForKey: @"recordName"];
    anInt   = [decoder decodeIntForKey:    @"recordInteger"];
    aBool   = [decoder decodeBoolForKey:   @"recordBool"];
}

In order to write the file, I have used the following:

[NSKeyedArchiver archiveRootObject:database toFile:myPath];

When I run the program, everything APPEARS to work correctly. The encoder method is called for each of the records in the array and the file is written to disk. Opening the file with TextEdit shows that the data is there (though mostly unintelligible to me.)

THE PROBLEM:

Here's where I run into a snag.

I added the following code to LOAD the file into my database array:

database = [NSKeyedUnarchiver unarchiveObjectWithFile:myPath];

When I run the program again, this time Loading the database, it APPEARS to work correctly. My first test was to use the NSMutableArray count method:

x = [database count];

The result was that X is filled with the correct number of records in the file. If there were 5 records when I saved the database, X is set to 5 after loading the database on the next execution of the program.

Now, here's the big problem:

The program crashes if I try to use ANY of my accessor methods. For example, if I try to use the following after loading the database:

aString = [[database objectAtIndex:someIndex] name];

the program crashes and returns the following error in the console:

Program received signal:  “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all

My interpretation is that the data is not being loaded and initialized into the database array correctly for some reason, but for the life of me I can't figure out where I've gone wrong here.

As a side note, everything I've implemented came from Stephen G. Kochan's book "Programming in Objective-C"

Any ideas would be greatly appreciated!

like image 326
Greg Steiner Avatar asked Apr 30 '12 23:04

Greg Steiner


2 Answers

There are a few problems with your code.

Your initWithCoder: method is not fully implemented. You must call [super init] and return self. You must also copy or retain the string object, otherwise it will be autoreleased:

- (id)initWithCoder:(NSCoder *)decoder 
{
    self = [super init];
    if(self)
    {
         name    = [[decoder decodeObjectForKey: @"recordName"] copy];
         anInt   = [decoder decodeIntForKey:    @"recordInteger"];
         aBool   = [decoder decodeBoolForKey:   @"recordBool"];
    }
    return self;
}

The other problem is with this line:

database = [NSKeyedUnarchiver unarchiveObjectWithFile:myPath];

That is fine, except for two things:

  1. You're not holding a reference to the object, so it will be autoreleased.
  2. NSKeyedArchiver returns an immutable object, in this case an NSArray and not an NSMutableArray.

You need to do this:

database = [[NSKeyedUnarchiver unarchiveObjectWithFile:myPath] mutableCopy];

That will both retain the object (because it's a copy) and make the object an NSMutableArray.

like image 160
Rob Keniger Avatar answered Oct 04 '22 22:10

Rob Keniger


It doesn't appear that you're initializing the recordObjects in -initWithCoder:

Try something like this:

-(id) initWithCoder: (NSCoder *) decoder {

    self = [super init];

    if (self){

       name    = [decoder decodeObjectForKey: @"recordName"] copy];
       anInt   = [decoder decodeIntForKey:    @"recordInteger"];
       aBool   = [decoder decodeBoolForKey:   @"recordBool"];
     }

    return self;
}

The data is there when you archive it but you're not properly unarchiving it.

like image 31
Brian Palma Avatar answered Oct 04 '22 21:10

Brian Palma