3
如果我在函數內部創建一個對象並返回它,何時應該釋放這個對象?這裏是我創建一個對象的例子。發佈對象正確返回?
- (NSDictionary*) sampleFunction
{
NSMutableDictionary* state = [[NSMutableDictionary alloc] initWithCapacity:5];
[state setObject:[[NSNumber alloc] initWithInt:self.a] forKey:@"a"];
[state setObject:[[NSNumber alloc] initWithInt:self.b] forKey:@"b"];
[state setObject:[[NSNumber alloc] initWithInt:self.c] forKey:@"c"];
return state;
}
附加問題:爲了避免內存泄漏,我也應該釋放這裏分配的NSNumbers?這段代碼看起來像沒有內存泄漏?
這非常有幫助,謝謝。 – Mark 2011-05-02 10:05:15