Search code examples
objective-cclassmethodsnsdictionaryanagram

Call class method and return a dictionary on objective c category


I have made a category in my x-code project like below:

+ (NSDictionary *)anagramMap {
static NSDictionary *anagramMap;
if (anagramMap != nil)
    return anagramMap;

// this file is present on Mac OS and other unix variants
NSString *allWords = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                               encoding:NSUTF8StringEncoding
                                                  error:NULL];




NSMutableDictionary *map = [NSMutableDictionary dictionary];
@autoreleasepool {
    [allWords enumerateLinesUsingBlock:^(NSString *word, BOOL *stop) {
        NSString *key = [word anagramKey];
        if (key == nil)
            return;
        NSMutableArray *keyWords = [map objectForKey:key];
        if (keyWords == nil) {
            keyWords = [NSMutableArray array];
            [map setObject:keyWords forKey:key];
        }
        [keyWords addObject:word];
    }];
}

anagramMap = map;
return anagramMap;}


  - (NSString *)anagramKey {
NSString *lowercaseWord = [self lowercaseString];

// make sure to take the length *after* lowercase. it might change!
NSUInteger length = [lowercaseWord length];

unichar sortedWord[length];
[lowercaseWord getCharacters:sortedWord range:(NSRange){0, length}];

qsort_b(sortedWord, length, sizeof(unichar), ^int(const void *a, const void *b) {
    unichar c1 = *(const unichar *)a;
    unichar c2 = *(const unichar *)b;
    if (c1 > c2)
        return -1;
    if (c1 < c2)
        return 1;
    return 0;
});

return [NSString stringWithCharacters:sortedWord length:length];}

Basically, this code loops through the Mac OSx dictionary and turns it into an NSDictionary where the key is the alphabetically sorted word, and the object is an array of all the anagrams of that word.

What I was wondering, is how can I call this method, such as in the viewDidLoad part of an implementation file which would assign an NSDictionary (or mutable) this created dictionary of the sorted key and object array? Basically in pseudo code i do something like:

NSMutatableArray *englishDictionary = [[NSMutableArray alloc] init]; englishDictionary = [NSMutableArray anagramMapScrabble]; //Should mean now englishDictionary has turned into the NSDictionary where the key = sorted word and object is an array of English anagrams of that sorted word

I think I'd have to put some extra code in the methods but I'm not sure. Any suggestions would be much appreciated!


Solution

  • A category is a modification of an existing class. It works exactly the same as if the methods of the category were declared in that class - because they are. So it works like every other method. If you make a category on, say, NSObject declared like this:

    + (NSDictionary *)anagramMap;
    

    Then that is a class method of NSObject and you call it by saying:

    NSDictionary* d = [NSObject anagramMap];
    

    If you make a category on NSObject declared like this:

    - (NSDictionary *)anagramMap;
    

    Then that is an method of NSObject and you call it by saying:

    NSObject* o = [NSObject new];
    NSDictionary* d = [o anagramMap];