如何从目标c中的Nsarray中删除空值或Null值
这里是我的示例代码:
cell.subtitleLbl.text = [[chapterArray objectAtIndex:indexPath.row] valueForKey:@"person"];
答案 0 :(得分:1)
<强>选择-1 强>
// mainArray array is your current array
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
for (int i = 0; i < [mainArray count]; i++) {
id obj = [mainArray objectAtIndex:i];
if (![obj isKindOfClass:[NSNull class]]) { // or if (![obj isKindOfClass:[[NSNull null]class]]) {
[tempArray addObject:obj];
}
}
NSLog(@"%@",tempArray);
<强>选择-2 强>
[yourarrayName removeObjectIdenticalTo:[NSNull null]];
<强>选择-3 强>
ussing Predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF CONTAINS %@)", @"<null>"];
[yourarrayname filterUsingPredicate:predicate];
<强>选择-4 强>
如果你使用字典数组
NSMutableArray *newarray=[NSMutableArray array];
for (NSDictionary *dictionary in presentArrray)
{
if (dictionary[key] != [NSNull null]) {
[newarray addObject:dictionary];
}
}
答案 1 :(得分:1)
使用可以使用可变阵列
从中删除空对象
[arrayName removeObjectIdenticalTo:[NSNull null]];