首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSKeyedUnarchiver -不良访问

NSKeyedUnarchiver -不良访问
EN

Stack Overflow用户
提问于 2010-05-18 09:15:53
回答 1查看 2K关注 0票数 2

我试图将一个对象归档到一个plist文件中,然后加载它以填充一个tableView。文件似乎被正确归档,但是当我试图从文件中获取值时,我得到了一个错误的访问权限。

我做错了什么吗?

这就是我保存它的地方

代码语言:javascript
复制
// 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"];

在这里,我尝试将它从文件中取出并保存回一个数组中。

代码语言:javascript
复制
- (void)viewDidLoad {
    // Load Phone Book
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:@"phonebook.plist"];

    self.list = arr;

    [arr release];
    [super viewDidLoad];
}

我试着建一个细胞

代码语言:javascript
复制
- (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断言失败:(cls),函数/SourceCache/objc4_Sim/objc4-427.5/runtime/objc-runtime-new.mm,,文件

第3990行。断言失败:(cls),函数getName,文件getName行3990。断言失败:(cls),函数getName,文件getName行3990。断言失败:(cls),函数getName,文件getName行3990.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-05-18 09:41:44

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

(我假设self.list属性是适当保留的,这样只要不在这里发布,对象就会保持在周围。如果没有,你也需要解决这个问题。)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2855859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档