how to remove NULL values from NSMutableArray? ios
Asked Answered
A

6

7

i have array of birthdates as array is getting filled from facebook so there are some friends whos birthdates are private so it contain NULL how to convert that array like empty string wherever there is null value the array is like below

"<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",
Agnes answered 8/3, 2013 at 10:37 Comment(1)
i think im stupid and this all are strings and <null> is also string.Agnes
H
14

The null values appear to be string literals @"<null>" rather than the NSNull objects typically used to represent nils in Cocoa collections. You can filter them out by using NSArray's filteredArrayUsingPredicate method:

NSArray *filtered = [original filteredArrayUsingPredicate:pred];

There are several ways of making the pred, one of them is

NSPredicate *pred = [NSPredicate predicateWithBlock:^BOOL(id str, NSDictionary *unused) {
    return ![str isEqualToString:@"<null>"];
}];
Hardheaded answered 8/3, 2013 at 10:42 Comment(1)
As mentioned by @dasblinkenlight , this pred also only checks if the string is equal to the string value "<null>". If you believe that the [NSNull null] value exists in your array, then you should just return str != [NSNull null]. ALSO: This is not the same as returning str != nilChicane
C
12

You have to use this to remove the actual [NSNull null] value.

 [array removeObjectIdenticalTo:[NSNull null]];
Compliment answered 24/6, 2013 at 18:49 Comment(0)
M
3

This works for me:

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]);
Mireyamiriam answered 8/3, 2013 at 10:42 Comment(1)
This only filters occurrences of the word "<null>", not the actual NSNull value.Compliment
S
1

In order to remove null values use :

[yourMutableArray removeObjectIdenticalTo:[NSNull null]];

You don't need iterate over.

Spaceband answered 1/6, 2016 at 17:19 Comment(1)
At least give credit to the original answer: https://mcmap.net/q/850051/-how-will-i-be-able-to-remove-nsnull-null-objects-from-nsmutablearraySarabia
T
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);
            }
       }
Tamartamara answered 6/8, 2017 at 23:36 Comment(1)
While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run.Immure
F
0

For json response I removed null values like this

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

}

Now remove empty values

//Add arrays to mutable array to remove empty objects

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];
          }
      }
}
Frown answered 8/3, 2018 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.