2011-04-22 46 views
2

我有这样iPhone如何在这种情况下释放内存

- (NSDictionary *)getCellValuesForRow:(int)row { 
NSMutableDictionary *dictValues= [[NSMutableDictionary alloc] init]; 

Outage *outage = [listOutage objectAtIndex:row]; 

[dictValues setObject:outage.duration forKey:@"OutageDuration"]; 
    return dictValues; 

}

和该值的方法被存贮在这样

NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:[self getCellValuesForRow:(indexPath.row-1)]]; 

如何在这个释放内存场景

回答

1

您应该在getCellValuesForRow中autorelease dictValues,或者不分配它。这将保持它自动释放:

NSMutableDictionary *dictValues= [NSMutableDictionary dictionary]; 

在大多数情况下,它应该是什么调用它的Alloc它(如果它需要自动释放池清空后要保持左右),再后来的dealloc它的责任。

如果任何调用它不需要它,它可以让它自动释放。

2

这是autorelease的用途。

NSMutableDictionary *dictValues= [[[NSMutableDictionary alloc] init] autorelease]; 
0

另一种方法是简单地使用

NSMutableDictionary *dictValues= [NSMutableDictionary dictionary]; 

这实际上是同样的事情有什么建议丹。少打字。

它适用于你的下一行,太:

NSDictionary *dict = [NSDictionary dictionaryWithDictionary:[self getCellValuesForRow:(indexPath.row-1)];