我有哪裏我聲明瞭一個NSMutableArray,然後測試它像這樣的情況:在Objective C中,聲明的變量是否等於nil?
-(void)whatever {
NsMutableArray *array;
for(int i = 0; i < 10; i++) {
if(array){
[array release];
}
array = [[NsMutableArray alloc] init];
// Add things to the array and do stuff with those things before starting over
}
if ([array count] > 0) {
// Do something else
}
}
出於某種原因,if(array){}
被評估爲是和它試圖釋放對象不存在尚未對第一次通過。如果我嘗試if(array != nil){}
,它也評估爲YES。有沒有更好的方法來測試這個?
[在Objective-C中,是沒有被賦值爲零的int變量的可能重複嗎?](http://stackoverflow.com/questions/3114952/in-objective-c-are-int-variables -that-havent-been-assigned-a-value-nil) – 2012-03-20 17:32:45
可能的重複[指針總是設置爲無聲明?](http://stackoverflow.com/questions/9071990/are-pointers-always-set對零宣佈) – Monolo 2013-04-09 14:06:57