释放NSMutableArray不会影响数组中元素的数量

Nar*_*rwe 3 memory-management objective-c 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
Run Code Online (Sandbox Code Playgroud)

我的计数不应该是零吗?'release'不会从数组中删除所有元素吗?

das*_*ght 6

count未定义,因为在release数组的最后访问属性之后是非法的:实质上,您正在访问悬空指针.

如果要清除阵列而不使其无效,请使用removeAllObjects方法.