2011-04-22 43 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)];