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.
<
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:
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
.