Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stray @ in program with NSDictionary definition

In the course of following a howto online, I came across the following code:

NSDictionary *address = @{
  (NSString *)kABPersonAddressStreetKey: _address.text,
  (NSString *)kABPersonAddressCityKey: _city.text,
  (NSString *)kABPersonAddressStateKey: _state.text,
  (NSString *)kABPersonAddressZIPKey: _zip.text
};

Which will not compile in XCode 4.5.1. I get two errors:

  1. Stray '@' in program
  2. Expected '}' before ':' token

What am I missing here? Also, where do I find documentation on this shortcut declaration of an NSDictionary object? It's awfully hard to Google syntax like this.

like image 342
jwoww Avatar asked Dec 11 '22 21:12

jwoww


1 Answers

This is part of Obj-C Literals, introduced since LLVM 4.0. Make sure you are using the right version of iOS as well. Translated into original pre-llvm 4.0 language, the NSDictionary assignment would look like this:

NSDictionary *address = [[NSDictionary alloc] initWithObjectsAndKeys:
                        (NSString *)kABPersonAddressStreetKey, _address.text, 
                        (NSString *)kABPersonAddressCityKey, _city.text,  
                        (NSString *)kABPersonAddressStateKey, _state.text, 
                        (NSString *)kABPersonAddressZIPKey, _zip.text, 
                        nil];
like image 91
Ravi Avatar answered Jan 05 '23 01:01

Ravi