Search code examples
iphoneiosnsmutablearraynsnull

how to remove NULL values from NSMutableArray? ios


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",

Solution

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