Search code examples
objective-cnsdictionarynsset

Setting up a NSDictonary with NSSet objects


Is there a way to setup a NSSDictionary with NSSet objects in one command? Or, do objects need to be created for all of the sets first?

For example, is something like this possible?

NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:{@"orange",@"lemon"},@"citrus",{@"horse",@"moose",@"goose"},@"animals",nil];

Thanks for reading.


Solution

  • There is no NSSet literal, no. But you can use an NSArray literal to initialise an NSSet… it's about the shortest you can get:

    NSDictionary *dictionary = @{
        @"citrus":  [NSSet setWithArray:@[@"orange", @"lemon"]],
        @"animals": [NSSet setWithArray:@[@"horse", @"moose", @"goose"]]};