2012-07-19 40 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后是非法的:实际上,您正在访问一个悬挂指针。

如果您想在不使其失效的情况下清除阵列,请使用​​方法。