NSKeyedUnarchiver - 访问不佳

时间:2010-05-18 09:15:53

标签: iphone nskeyedarchiver

我试图将对象存档到plist文件中,然后加载它以填充tableView。 看起来该文件已正确归档,但在尝试从文件中获取值时,我获得了错误的访问权。

我做错了吗?

这是我保存的地方

// 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"];

在这里,我尝试将其从文件中删除并将其保存回数组

- (void)viewDidLoad {
    // Load Phone Book
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:@"phonebook.plist"];

    self.list = arr;

    [arr release];
    [super viewDidLoad];
}

我尝试构建单元格的部分

- (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;
}

这里是错误的访问错误

  

当前语言:auto;目前   objective-c断言失败:(cls),   function getName,file   /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm,   第3990行。断言失败:(cls),   function getName,file   /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm,   第3990行。断言失败:(cls),   function getName,file   /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm,   第3990行。断言失败:(cls),   function getName,file   /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm,   第3990行。

1 个答案:

答案 0 :(得分:2)

只需扩展一小部分:unarchiveObjectWithFile将返回一个自动释放的指针。你不在本地retain,所以你不应该release。因为你这样做了,所以该对象随后被释放,当你通过调用book.name来使用它时,它就不存在了。

(我假设self.list属性保持适当,以便只要您不在此处释放,对象就会被保留。如果没有,您也需要修复它。)< / p>