我有生辰八字的阵列阵列接收来自Facebook充满所以有一些朋友卫生组织生日是私人所以它包含NULL如何数组像空字符串转换,只要有空值的数组类似于下面
"<null>",
"10/29/1988",
"11/13",
"03/24/1987",
"04/25/1990",
"03/13",
"01/01",
"<null>",
"12/15/1905",
"07/10",
"11/02/1990",
"12/30/1990",
"<null>",
"07/22/1990",
"01/01",
"07/17/1989",
"08/28/1990",
"01/10/1990",
"06/12/1990",
的null
值似乎是字符串文字@"<null>"
而非NSNull
对象通常用于表示nil
可可集合秒。 您可以使用筛选出来NSArray
的filteredArrayUsingPredicate
方法:
NSArray *filtered = [original filteredArrayUsingPredicate:pred];
有制作的几种方法pred
,其中之一是
NSPredicate *pred = [NSPredicate predicateWithBlock:^BOOL(id str, NSDictionary *unused) {
return ![str isEqualToString:@"<null>"];
}];
你必须使用这个删除实际[NSNull空]值。
[array removeObjectIdenticalTo:[NSNull null]];
这对我的作品:
NSMutableArray *array = [NSMutableArray arrayWithObjects:
@"<null>",
@"10/29/1988",
@"11/13",
@"03/24/1987",
@"04/25/1990",
@"03/13",
@"01/01",
@"<null>",
@"12/15/1905",
@"07/10",
@"11/02/1990",
@"12/30/1990",
@"<null>",
@"07/22/1990",
@"01/01",
@"07/17/1989",
@"08/28/1990",
@"01/10/1990",
@"06/12/1990", nil];
NSLog(@"%d", [array count]);
NSString *nullStr = @"<null>";
[array removeObject:nullStr];
NSLog(@"%d", [array count]);
为了消除空值使用:
[yourMutableArray removeObjectIdenticalTo:[NSNull null]];
你并不需要遍历。
for(int i = 0;[yourMutableArray count] > 0;i++){
if([yourMutableArray isKindOfClass:[NSNull class]]){ // indentifies and removes null values from mutable array
[yourMutableArray removeObjectAtIndex:i];
// or
[yourMutableArray replaceObjectAtIndex:i withObject:@"No date available"];
NSLog(@"*** %@",yourMutableArray);
}
}
对于JSON响应我删除这样的空值
NSArray *arr = [NSArray arrayWithObjects:_IDArray, _TypeArray, _NameArray, _FlagArray, nil];
for (int i=0; i<_integer; i++) {
// My json response assigned to above 4 arrayes
//Now remove null values
//Remove null values
for (int j=0; j<arr.count; j++) {
for (NSMutableArray *ar in arr) {
if ([[ar objectAtIndex:i] isKindOfClass:[NSNull class]] || [[ar objectAtIndex:i] isEqualToString:@"null"]) {
[ar addObject:@""];//Add empty value before remove null value
[ar removeObjectAtIndex:i];
}
}
}
}
现在,删除空值
//添加阵列可变数组以除去空物体
NSArray *marr = [NSArray arrayWithObjects:_IDArray, _TypeArray, _NameArray, _FlagArray, nil];
//Remove empty objects from all arrays
for (int j=0; j<marr.count; j++) {
for (int i=0; i<[[marr objectAtIndex:j] count]; i++) {
if ([[[marr objectAtIndex:j] objectAtIndex:i] isEqualToString:@""]) {
[[marr objectAtIndex:j] removeObjectAtIndex:i];
}
}
}