Search code examples
iosnsmutablearraynsarraynsset

Create a unique NSArray from NSSet with out null values


I am trying to create an NSArray of sorted unique values excluding NULL values from an array of NSdictionaries I currently have. However I can do everything except remove the NULL values how would I do achieve this?

This is the code I currently have.

NSSet *uniqueAreaSet = [NSSet setWithArray:[tempArray valueForKey:@"stage"]];
    NSArray *tempAreaArray = [uniqueAreaSet allObjects];
    NSArray *sortedAreaArray = [tempAreaArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

Solution

  • For removing the null values (if they are actually NSNull objects) you can use the following:

    sortedAreaArray = [sortedAreaArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
            return ![evaluatedObject isEqual:[NSNull null]];
        }]];