Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

"syntax error before '@' token" on NSDictionary in command-line utility

Hi - I'm playing around with building a command-line utility in XCode. However, when I attempt to initialize an NSDictionary I get the following error:

"syntax error before '@' token".

Here's the code:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@”red”, @”color1”, nil];

[pool drain];
return 0;
}

The NSDictionary code is identical to example code provided in the XCode docs, and to other code snippets I've found on the web. So, I'm pretty confident that it's not a code problem...

Maybe it's my setup in XCode??
XCode version: 3.1.4
SDK: Mac OSX 10.5 (SDK Base)
Active Configuration: Debug
Under "Targets" > "Link Binary With Libraries" > Foundation.framework > Headers (includes NSDictionary.h)

I downloaded the iPhone SDK (I'm learning iPhone development). Could it be that I can't compile to Mac OSX with a command-line utility with just the iPhone SDK?

Any other ideas?

Thanks in advance!

Mac OS X (10.5)

Posted on Mar 2, 2010 10:34 AM

Reply
17 replies

Mar 2, 2010 10:39 AM in response to golddigger1

Hello gd1 & welcome to the forums

+>Could it be that I can't compile to Mac OSX with a command-line utility with just the iPhone SDK?+

What happens when you use Xcode's GUI instead....

I'm pretty sure the results should be the same (fail), but you should perhaps eliminate the obvious on your own as part of learning, if you haven't already.

Mar 2, 2010 4:53 PM in response to golddigger1

Did the WhatATool tutorial include a .xcodeproj file that was created under an older version of XCode (ie older than 3.1.4)? If so, that's the root of your problem and the only way out is to recreate the project from scratch under 3.1.4, or install the older XCode that matches the provided .xcodeproj.

Mar 3, 2010 6:26 AM in response to golddigger1

golddigger1 wrote:
Hey, good guess! Yes, I am following the Stanford CS193P curriculum...But, to clarify my previous statement - I didn't actually get the xproj file - I created it myself. The template I used when creating the project was "Command Line Utility" > "Foundation Tool".


What else are you doing after creating the project? If I create a new project from the Foundation Tool, select your text above, and paste it into the "main.m" file, it works perfectly. What are you doing?

Mar 3, 2010 4:56 PM in response to etresoft

Tried opening a brand new project from scratch. (File > New Project) At this point you're presented with the "Choose a template for your new project" dialogue box". I select "Command Line Utility" from the Mac OS X list. Also I select the "Foundation Tool" icon to the right, in the dialogue box. Let's say I call my project "Test". After hitting ok, the Test project is created. I then edit "Test.m" by inserting the single line of code

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@”red”, @”color1”, nil];

And hit Build > Build & Run.

That's it.

Think I will try upgrading XCode. Maybe there is something wrong with the install or the libraries...

Mar 4, 2010 9:10 AM in response to golddigger1

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@”red”, @”color1”, nil];


Yes, the NSDictionary enclosed in square brackets. In the previous posts, that statement has a line through it. I just noticed that code must be wrapped like this -
code here
.

So, I am assuming that you are already doing this and that you do not have curly style quotes, like you sometimes get when copying from a website and that you have terminated the line with a semi-colon. Those two can cause errors.

If that is the case. You may want to post the entire code from main.m file.

Mar 4, 2010 9:28 AM in response to Craig.Williams

Right. Yes, the NSDictionary statement is enclosed in square brackets. Here's the complete .m file:

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@”red”, @”color1”, nil];

// insert code here...
NSLog(@"Hello, World!");
[pool drain];
return 0;
}


By the way, downloaded and reinstalled iPhone SDK 3.1.2 and Xcode 3.1.4 that comes with it. Still getting the same error...

Mar 4, 2010 9:58 AM in response to golddigger1

The quotes you are using around red and color1 are curly quotes. See the difference between the highlighting of mine and yours? Mine are red and yours are black. Fix that and it should work fine.


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"red", @"color1", nil];

// insert code here...
NSLog(@"Hello, World!");
[pool drain];
return 0;
}

"syntax error before '@' token" on NSDictionary in command-line utility

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.