Skip to content

Instantly share code, notes, and snippets.

@donjordano
Forked from mz2/NSArray+UniqueObjects.h
Created March 16, 2014 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donjordano/9586858 to your computer and use it in GitHub Desktop.
Save donjordano/9586858 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface NSArray (UniqueObjects)
-(NSArray*) uniqueObjects;
-(NSArray*) uniqueObjectsSortedUsingSelector: (SEL)comparator;
-(NSArray*) uniqueObjectsSortedUsingFunction: (NSInteger (*)(id, id, void *)) comparator
context: (id)context
hint: (id)hint;
-(NSArray*) uniqueObjectsSortedUsingFunction: (NSInteger (*)(id, id, void *)) comparator
context: (id)context;
-(NSArray*) uniqueObjectsSortedUsingSortDescriptors: (NSArray*) sortDescs;
@end
// The implementation
#import "NSArray+UniqueObjects.h"
@implementation NSArray (UniqueObjects)
-(NSArray*) uniqueObjects {
NSSet *set = [[NSSet alloc] initWithArray: self];
NSArray *vals = [set allObjects];
[set release];
return vals;
}
-(NSArray*) uniqueObjectsSortedUsingSelector: (SEL)comparator {
NSSet *set =
[[NSSet alloc] initWithArray: self];
NSArray *vals =
[[set allObjects] sortedArrayUsingSelector: comparator];
[set release];
return vals;
}
-(NSArray*)
uniqueObjectsSortedUsingFunction:
(NSInteger (*)(id, id, void *)) comparator
context: (id)context
hint: (id)hint {
NSSet *set = [[NSSet alloc] initWithArray: self];
NSArray *vals =
[[set allObjects] sortedArrayUsingFunction: comparator
context: context
hint: hint];
[set release];
return vals;
}
-(NSArray*)
uniqueObjectsSortedUsingFunction:
(NSInteger (*)(id, id, void *)) comparator
context: (id)context {
NSSet *set = [[NSSet alloc] initWithArray: self];
NSArray *vals = [[set allObjects]
sortedArrayUsingFunction:comparator context:context];
[set release];
return vals;
}
-(NSArray*)
uniqueObjectsSortedUsingSortDescriptors: (NSArray*) sortDescs
{
NSSet *set = [[NSSet alloc] initWithArray: self];
NSArray *vals =
[[set allObjects] sortedArrayUsingDescriptors:sortDescs];
[set release];
return vals;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment