I just put together a quick example of how I approach this, but it turns out that this is weirder than you expect. First, NSCharacterSet does not perform equality to validate content. It uses only the value of a pointer. Based on this, your example will NOT work properly.
My approach is to use NSSet to handle hashing this data for us.
@interface StringWrapper : NSObject @property (nonatomic, copy) NSString *string; @property (nonatomic, copy) NSData *charSetBitmap; - (id)initWithString:(NSString*)aString; @end @implementation StringWrapper @synthesize string, charSetBitmap; - (id)initWithString:(NSString*)aString; { if ((self = [super init])) { self.string = aString; } return self; } - (void)setString:(NSString *)aString; { string = [aString copy]; self.charSetBitmap = [[NSCharacterSet characterSetWithCharactersInString:aString] bitmapRepresentation]; } - (BOOL)isEqual:(id)object; { return [self.charSetBitmap isEqual:[object charSetBitmap]]; } - (NSUInteger)hash; { return [self.charSetBitmap hash]; } @end int main (int argc, const char * argv[]) { @autoreleasepool { NSMutableSet *stringWrappers = [[NSMutableSet alloc] init]; NSArray *strings = [NSArray arrayWithObjects:@"abc",@"aaabcccc",@"awea",@"awer",@"abcde", @"ehra", @"QWEQ", @"werawe", nil]; for (NSString *str in strings) [stringWrappers addObject:[[StringWrapper alloc] initWithString:str]]; NSArray *uniqueStrings = [stringWrappers valueForKey:@"string"]; NSLog(@"%@", uniqueStrings); } return 0; }
The code is pretty simple. We create a container object to cache the results of the representation of a bitmap character set. We use a bitmap representation because NSData implements isEqual: accordingly.
source share