有人說可以[array count]==0 這樣來判斷是否為空,都是坑,如果array為空的話,執行count就會直接報錯,程式退出。
正确判斷NSArray是否為空的方法:!array
舉例代碼如下:
+(NSMutableArray *)createIllustration:(NSArray *)arr{
NSMutableArray *array = [NSMutableArrayarray];
if (arr) {
for (int i =0; i < arr.count; i++) {
NSDictionary *tempArr = [arr objectAtIndex:i];
Illustration *tempIll = [[Illustrationalloc]init];
tempIll.mid = [tempArrobjectForKey:@"comein_id"];
tempIll.mtag = [tempArrobjectForKey:@"tag"];
tempIll.mlittleImg = [tempArrobjectForKey:@"comein_app"];
tempIll.mdetailImg = [tempArrobjectForKey:@"comein_wap_logo"];
tempIll.mdescription = [tempArrobjectForKey:@"comein_description"];
tempIll.mtitle = [tempArrobjectForKey:@"comein_title"];
[arrayaddObject:tempIll];
}
// NSLog(@"NSArray:%@",array);
}else{
NSLog(@"參數資料為空!");
}
return array;
}