如何从NSMutableArray中删除NULL值? IOS

时间:2013-03-08 10:37:46

标签: iphone ios nsmutablearray nsnull

我有一系列的生日,因为数组正在从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",

6 个答案:

答案 0 :(得分:13)

null值似乎是字符串文字@"<null>",而不是通常用于表示Cocoa集合中NSNull的{​​{1}}个对象。您可以使用nil的{​​{1}}方法过滤掉它们:

NSArray

有几种方法可以制作filteredArrayUsingPredicate,其中一种是

NSArray *filtered = [original filteredArrayUsingPredicate:pred];

答案 1 :(得分:10)

您必须使用它来删除实际的[NSNull null]值。

 [array removeObjectIdenticalTo:[NSNull null]];

答案 2 :(得分:3)

这对我有用:

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]);

答案 3 :(得分:1)

要删除空值,请使用:

[yourMutableArray removeObjectIdenticalTo:[NSNull null]];

您不需要迭代。

答案 4 :(得分:1)

    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);
            }
       }

答案 5 :(得分:0)

对于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];
          }
      }
}