NSKeyedUnarchiver - Bad Access
I was trying to archive the object into a plist file and load it later to populate the tableView. The file seems to be zipped correctly, but I'm getting poor access when trying to get the value from the file.
Am I doing something wrong?
This is where I save it
// Create some phonebook entries and store in array
NSMutableArray *book = [[NSMutableArray alloc] init];
Phonebook *chris = [[Phonebook alloc] init];
chris.name = @"Christian Sandrini";
chris.phone = @"1234567";
chris.mail = @"christian.sandrini@example.com";
[book addObject:chris];
[chris release];
Phonebook *sacha = [[Phonebook alloc] init];
sacha.name = @"Sacha Dubois";
sacha.phone = @"079 777 777";
sacha.mail = @"info@yzx.com";
[book addObject:sacha];
[sacha release];
Phonebook *steve = [[Phonebook alloc] init];
steve.name = @"Steve Solinger";
steve.phone = @"079 123 456";
steve.mail = @"steve.solinger@wuhu.com";
[book addObject:steve];
[steve release];
[NSKeyedArchiver archiveRootObject:book toFile:@"phonebook.plist"];
And here I am trying to extract it from a file and store it back into an array
- (void)viewDidLoad {
// Load Phone Book
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:@"phonebook.plist"];
self.list = arr;
[arr release];
[super viewDidLoad];
}
The part where I am trying to create a cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *PhoneBookCellIdentifier = @"PhoneBookCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PhoneBookCellIdentifier];
if ( cell == nil )
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:PhoneBookCellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
Phonebook *book = [self.list objectAtIndex:row];
cell.textLabel.text = book.name;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
Here's a bad access error
Current language: auto; Currently objective-c Assertion failed: (cls), function getName, file / SourceCache / objc 4_Sim / objc4-427.5 / runtime / objc-runtime-new.mm, line 3990. Assertion failed: (cls), function getName, file / SourceCache / objc 4_Sim / objc4-427.5 / runtime / objc-runtime-new.mm, line 3990. Assertion failed: (cls), function getName, file / SourceCache / objc 4_Sim / objc4-427.5 / runtime / objc-runtime-new.mm, line 3990. Assertion failed: (cls), function getName, file / SourceCache / objc 4_Sim / objc4-427.5 / runtime / objc-runtime-new.mm, line 3990.
a source to share
Just to expand this faction: unarchiveObjectWithFile
will return an auto-implementing pointer. You are not local retain
, so you shouldn't release
. Since you do this, the object is subsequently freed, and by the time you use it by calling it book.name
, it doesn't exist.
(I am assuming the property is self.list
stored appropriately, so that the object will be kept until you drop it here. If not, you will need to fix that as well.)
a source to share