Search code examples
objective-cnsdictionaryobjective-c-literals

Objective-C at sign and curly braces, @{ ... } what does it mean?


I have this line in Objective-C.

NSMutableArray *mutableArray;
[mutableArray addObject:@{ @"Something" : aObject, @"Otherthing" : anotherObject }];

What does the @{ ... } part do exactly? It is an object, but it seems to create some kind of key, value pair on the fly.


Solution

  • It is creating NSDictionary object as you said. Syntax is simple

    NSDictionary* dictionary = @{key: object, key: object};
    

    In your example, keys are objects of NSString class. It is important to remember that dictionary copies keys and retains values.