Wednesday, April 25, 2012

Find all words in a sentence

This is one of the first samples in Mac OS X Developer Library. I found it in Cocoa Fundamental Guide



#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSArray *param = [[NSProcessInfo processInfoarguments];
        NSCountedSet *cset = [[NSCountedSet allocinitWithArray:param];
        NSArray *sorted_args = [[cset allObjects]
                                sortedArrayUsingSelector:@selector(compare:)];
        NSEnumerator *enm = [sorted_args objectEnumerator];
        id word;
        while (word = [enm nextObject]) {
            printf("%s\n", [word UTF8String]);
        }
        
        [cset release];
        
    }
    return 0;
}
here is the Terminal Window:


I payed attention on this sample, because it shows a useful trick: how to sort and enumerate data, for example an NSDictionary object.

No comments:

Post a Comment