2011-05-05 76 views
0

好吧,這就是我在做什麼。釋放已分配UIViews的NSMutableArray是否會釋放UIViews?

NSMutableArray *array = [[NSMutableArray alloc] init]; 

UIView *tempview = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)]; 
[array addObject:tempView]; 

UIView *tempview2 = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)]; 
[array addObject:tempView2]; 

[array release]; 

請問是否釋放數組,釋放兩個已分配的UIViews?

回答

2

如果copyallocretain,或new東西,你是負責發送,要麼releaseautorelease

[[UIView alloc] init...]所以必須release生成的對象。

2

您有責任在您創建視圖後發佈視圖。這是怎麼回事:

您創建保留計數爲1的視圖。 當它們被添加到數組時,它將保留它們(保留計數= 2)。 當您釋放數組時,它將釋放視圖(保留計數= 1)。 你仍然需要釋放它們。

正確的代碼是:

NSMutableArray *array = [[NSMutableArray alloc] init]; 

UIView *tempview = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)]; 
[array addObject:tempView]; 
[tempview release]; 

UIView *tempview2 = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)]; 
[array addObject:tempView2]; 
[tempview2 release]; 

[array release]; 
+0

作爲一個側面說明,意見將在這種情況下,的確可以與陣列的最終版本發佈。 – 2011-05-05 19:59:49