⬅ Back to Rob Mayoff's Site Index

Cocoa Programming Notes

It appears to be perfectly acceptable to set an NSArrayController's content object to an NSSet. For example:

NSSet* symbolSet = [stockMarket symbolSet];
[symbolListController setContent:symbolSet];

On a related note, suppose your NSArrayController's content is a set (or array) of NSStrings, and you want it to sort those strings by their values. You have to give the controller an NSSortDescriptor, and an NSSortDescriptor requires a key path specifying a property of the controlled objects. But in this case you don't want to sort by a property, you want to sort by the objects (NSStrings) themselves. It turns out that NSObject has a method, -(id)self, which returns self. So you can specify @"self" as the key path, like this:

NSSortDescriptor* sd = [[[NSSortDescriptor alloc]
    initWithKey:@"self" ascending:YES] autorelease];
[symbolListController setSortDescriptors:
    [NSArray arrayWithObject:sd]];

Note that specifying nil or @"" as the key path will not work.

blog comments powered by Disqus