NSKeyedArchiver and NSKeyedUnarchiver with NSMutableArray

回眸只為那壹抹淺笑 提交于 2019-12-03 11:04:26

问题


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!


回答1:


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.




回答2:


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.




回答3:


Sounds like a memory management issue to me. EXC_BAD_ACCESS usually means that you're trying to access an object that has been deallocated. unarchiveObjectWithFile: returns an autoreleased object, so you have to retain it if you want to keep it around, either with an explicit retain or by assigning it to a retained property.




回答4:


I was having the same issue when unarchiving a custom object

self.calTable = [[NSKeyedUnarchiver unarchiveObjectWithFile:calibrationFile] objectForKey:@"calTable"];

Based on Rob's answer I changed to

self.calTable = [[[NSKeyedUnarchiver unarchiveObjectWithFile:calibrationFile] mutableCopy] objectForKey:@"calTable"];

and it fixed all errors.



来源:https://stackoverflow.com/questions/10391803/nskeyedarchiver-and-nskeyedunarchiver-with-nsmutablearray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!