【问题标题】:NSKeyedUnarchiver - Bad accessNSKeyedUnarchiver - 访问错误
【发布时间】:2010-05-18 09:15:53
【问题描述】:

我试图将一个对象归档到一个 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;
}

这里是错误的访问错误

当前语言:自动;目前 objective-c 断言失败:(cls), 函数获取名称,文件 /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm, 第 3990 行。断言失败:(cls), 函数获取名称,文件 /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm, 第 3990 行。断言失败:(cls), 函数获取名称,文件 /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm, 第 3990 行。断言失败:(cls), 函数获取名称,文件 /SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm, 第 3990 行。

【问题讨论】:

  • 我不确定这是否是唯一的问题,但您不应该在 viewDidLoad 中释放 arr。

标签: iphone nskeyedarchiver


【解决方案1】:

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

(我假设 self.list 属性已适当保留,因此只要您不在这里释放,该对象就会一直保留。如果没有,您也需要修复它。)

【讨论】:

  • 你是男人 ;-) 删除 [arr release] 解决了这个问题。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2020-01-20
  • 2019-08-15
  • 2014-06-22
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多