@distinctUnionOfObjects returns only the unique value of the object indicated by this property, not the objects themselves.
You can try the following:
NSDictionary *arnold = @{@"name" : @"arnold", @"state" : @"california"}; NSDictionary *jimmy = @{@"name" : @"jimmy", @"state" : @"new york"}; NSDictionary *henry = @{@"name" : @"henry", @"state" : @"michigan"}; NSDictionary *woz = @{@"name" : @"woz", @"state" : @"california"}; NSArray *people = @[arnold, jimmy, henry, woz]; NSMutableArray *uniqueArray = [[NSMutableArray alloc] init]; NSMutableSet *checkedStates = [[NSMutableSet alloc] init]; for (NSDictionary *person in people) { NSString *currentStateName = person[@"state"]; BOOL isDuplicateState = [checkedStates containsObject:currentStateName]; if (!isDuplicateState) { [uniqueArray addObject:person]; [checkedStates addObject:currentStateName]; } } NSLog(@"Results %@", uniqueArray);
Exiting NSLog will be:
Results ( { name = arnold; state = california; }, { name = jimmy; state = "new york"; }, { name = henry; state = michigan; } )
source share