3

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
unom
  • 11,438
  • 4
  • 34
  • 54
  • 1
    First Google result: http://iosdevelopertips.com/objective-c/nsdictionary-literals-in-objective-c.html – NathanAldenSr Mar 19 '14 at 15:44
  • 1
    @NathanAldenSr I think this is beyond a simple google search, unless you actually know what it is called/is your not going to get very good google results. I googled the meaning of it and got that link but then dummed it down to what a newbie may search for and didn't get anything close to what this actually is. – Popeye Mar 19 '14 at 15:48
  • True, if only I knew the right question?... tried a couple of keywords in google and couldn't get it to display the right stuff. I knew it was there. – unom Mar 19 '14 at 15:48
  • Yeah, you're right. It was the first Google result for the phrase *nsdictionary shorthand*. I should've been more specific. – NathanAldenSr Mar 19 '14 at 15:50
  • 1
    OH... now I learn something new... this is the "shorthand" way to create an NSdictionary! Great. Thanks! – unom Mar 19 '14 at 15:52
  • And you can use `@[value, value, value]` to create an NSArray. – Hot Licks Mar 19 '14 at 15:57

3 Answers3

6

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.

Michał Banasiak
  • 950
  • 6
  • 13
  • Thank you for the answer. Could you elaborate on the last part... what do you mean it "copies" keys and "retains" values. I think i get the "retain" part, I'm thinking it just increases the retain count on the value object and stores a reference to it. From this I deduce that on the key side it is doing the opposite? Why this? – unom Mar 19 '14 at 15:51
  • Copy means just what it says. It creates an immutable copy of the key objects in case they're mutable, and subsequently change, which would be a bad thing. This does mean that the keys used in an NSDictionary must implement the NSCopying protocol. – Andrew Madsen Mar 19 '14 at 15:59
3

These are called Literals. Apple LLVM Compiler 4.0 and above can use this.

In your question, the expression creates a dictionary

NSDictionary *settings = @{ AVEncoderAudioQualityKey : @(AVAudioQualityMax) };

Similarly arrays which were created using NSArray arrayWithArray and other similar methods, can now be done easily

NSArray *array = @[ @"Hello", @"World"]; 

and you will not even need the nil sentinel.

More details here: http://clang.llvm.org/docs/ObjectiveCLiterals.html

Anindya Sengupta
  • 2,539
  • 2
  • 21
  • 27
  • Actually, it's not literally a literal, since the keys and values can be variables. But it's the same basic idea. – Hot Licks Mar 19 '14 at 15:56
1

The @{ ... } syntax is a shorthand way of creating a NSDictionary introduced as part of Modern Objective-C. The syntax @{@"key1": object1, @"key2": object2} is just a shorthand for more verbose methods like [NSDictionary dictionaryWithObjectsAndKeys:] among a few others.

James Kuang
  • 10,710
  • 4
  • 28
  • 38
  • In other words: It's a *Godsend*. :) – NathanAldenSr Mar 19 '14 at 15:51
  • Of course, under the covers, it's just executing a private form of `dictionaryWithObjectsAndKeys`. It's just a more convenient way to write it. – Hot Licks Mar 19 '14 at 15:55
  • I'm new to Objective-C and I beg to differ. I could actually read [NSDictionary dictionaryWithObjectsAndKeys:aObject, key1 ,anotheObject, key2, nil]... now I can't even do that. – unom Mar 19 '14 at 16:03
  • You can keep using that method if you want. Anyone with any experience, however, will likely prefer to have keys and values in the right order and not have a sentinel. – Jon Shier Mar 19 '14 at 16:05