开发者

NSKeyedUnarchiver - Bad access

I was trying to archive an object into a plist file and load it later to fill a tableView. It seems that the file gets archived correctly but I get a bad access when trying to get the value out of 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 45开发者_运维问答6";
steve.mail = @"steve.solinger@wuhu.com";
[book addObject:steve];
[steve release];

[NSKeyedArchiver archiveRootObject:book toFile:@"phonebook.plist"];

And here I try to get it out of the file and save 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 try to build 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 the bad access error

Current language: auto; currently objective-c Assertion failed: (cls), function getName, file /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm, line 3990. Assertion failed: (cls), function getName, file /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm, line 3990. Assertion failed: (cls), function getName, file /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm, line 3990. Assertion failed: (cls), function getName, file /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm, line 3990.


Just to expand on that a fraction: unarchiveObjectWithFile will return an autoreleased pointer. You don't locally retain it so you shouldn't release. Because you do, the object is subsequently deallocated and by the time you come to use it by calling book.name, it's not there.

(I'm assuming the self.list property is retaining appropriately so that the object will be kept around as long as you don't release here. If not, you'll need to fix that too.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜