发布NSMutableDictionary时出现问题

时间:2009-04-04 12:13:28

标签: iphone objective-c cocoa-touch

当我尝试发布字典时,我得到一个例外。

这是我的代码:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (!tableDataDictionary)
{
    DebugLog(@"initializing tableDataDictionary");
    tableDataDictionary = [ [NSMutableDictionary alloc] init];
}
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [mainTableView deselectRowAtIndexPath: [mainTableView indexPathForSelectedRow] animated:NO];

    [tableDataDictionary release];
}

我该如何解决?

1 个答案:

答案 0 :(得分:5)

最有可能你需要淘汰tableDataDictionary实例变量。否则,第一次运行这些方法时,它将正常工作,但第二次,tableDataDictionary将不会为nil,并将指向dealloc'd指针;因此不会进行alloc调用,并且当调用viewWillDisappear:时,它将尝试再次释放该指针。所以,解决它:

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [mainTableView deselectRowAtIndexPath: [mainTableView indexPathForSelectedRow] animated:NO];

[tableDataDictionary release];
tableDataDictionary = nil;

}