我试图将一个对象归档到一个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断言失败:(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.
发布于 2010-05-18 09:41:44
为了扩展这个分数:unarchiveObjectWithFile将返回一个自动释放的指针。您不是在本地retain它,所以您不应该release。因为这样做了,该对象随后就会被释放,当您开始通过调用book.name来使用它时,它就不在了。
(我假设self.list属性是适当保留的,这样只要不在这里发布,对象就会保持在周围。如果没有,你也需要解决这个问题。)
https://stackoverflow.com/questions/2855859
复制相似问题