2012-07-19 38 views
3

有人可以向我解釋下面的結果嗎?釋放NSMutableArray不影響數組中元素的數量

//generate an array with 4 objects 
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: 
         [NSNumber numberWithInt:1], 
         [NSNumber numberWithInt:2], 
         [NSNumber numberWithInt:3], 
         [NSNumber numberWithInt:4], 
         nil]; 
//release the array  
[array release]; 

//get a count of the number of elements in the array 
int count = [array count]; <--- count returns 4 

我的計數不應爲零嗎? '釋放'不會從陣列中刪除所有元素嗎?

回答

6

count的值未定義,因爲訪問數組的最後release後是非法的:實際上,您正在訪問一個懸掛指針。

如果您想在不使其失效的情況下清除陣列,請使用​​方法。