How to remove NULL values ​​from NSMutableArray? IOS

I have an array of birthdays since the array is populated from facebook, so there are some friends whose birthdays are private, so it contains NULL, how to convert this array to an empty string where there is a null value, the array looks 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", 
+6
source share
6 answers

null values ​​look like string literals @"<null>" , not the NSNull objects that are commonly used to represent nil in Cocoa collections. You can filter them using the NSArray filteredArrayUsingPredicate method:

 NSArray *filtered = [original filteredArrayUsingPredicate:pred]; 

There are several ways to create pred , one of which is

 NSPredicate *pred = [NSPredicate predicateWithBlock:^BOOL(id str, NSDictionary *unused) { return ![str isEqualToString:@"<null>"]; }]; 
+13
source

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

  [array removeObjectIdenticalTo:[NSNull null]]; 
+9
source

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]); 
+3
source

To remove null values, use:

 [yourMutableArray removeObjectIdenticalTo:[NSNull null]]; 

You do not need to sort out.

+1
source
  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); } } 
0
source

For json answer, 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 delete the empty values

// Add arrays to a 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]; } } } 
0
source

All Articles