9

All of the sudden Xcode is giving me an error "unexpected '@' in program" at the beginning of my object @interface.

This is happening in a bunch of my objects that were previously working...

Here's an example (with errors in comments)

#import <UIKit/UIKit.h>
#import "ONCOChordDiamond.h"
@interface ONCOChordView : UIView   //// unexpected '@'  in program
{
    NSMutableArray* chordDiamonds;    
    NSUInteger      diamondWidth, diamondHeight;
}

@end                                /////unexpected '@' in program'

So why is Xcode giving me this error? It seems like a bug.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
olynoise
  • 2,016
  • 2
  • 19
  • 32

6 Answers6

12

I doubt you're still having the issue, but if you are check the header file. I pasted in code from the web in a class and the quotation marks weren't the standard " ". The marks were stylized with an opening quote and a closing quote because it came from a webpage. Retyping the quote marks fixed it.

Aaron Douglas
  • 2,494
  • 1
  • 17
  • 14
  • Thanks for posting this comment; I struggled with a similiar issue for several hours and your comment resolved my issue. Thanks. – 5lb Bass Oct 01 '12 at 14:54
5

You are including the header from a file that is not compiled as Objective C.

Skyler Saleh
  • 3,961
  • 1
  • 22
  • 35
  • I had this problem while trying to #import the header of an NSString-categorty in an Objective-C++ (.mm) implementation-file. It worked fine however after i moved the #import line to the corresponding header file. Why that is? – arri Mar 15 '14 at 13:17
5

Check for syntax errors in the ONCOChordDiamond.h file. They may be highlighted for you if you run Product > Analyze in Xcode.

Importing a file with syntax errors could lead to the compiler not being able to parse the current file correctly, even if the current file's syntax is correct.

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
0

When encountering this problem with NSString literals, Check that you have used quotation marks, as required.

 (eg. @"myString")

If you find your NSStrings are not correctly syntax coloured, you might check for the following offending characters.

" (QUOTATION MARK) 
″ (DOUBLE PRIME) 
“ (LEFT DOUBLE QUOTATION MARK) 
” (RIGHT DOUBLE QUOTATION MARK)
Slowburner
  • 183
  • 3
  • 11
0

For what it's worth, I had this issue and the offending line that was identified was nowhere near any string literals.

I did notice that some of the following lines were colored oddly (again, no string literals to blame), so I tested out putting a bogus string literal in the code just before the offending line.

NSString *whatever = @"";

Apparently, it convinced the compiler that the line wasn't screwed up, so it then compiled just fine without complaining. After that build, I was able to delete the bogus string variable and move on with my life.

mbm29414
  • 11,558
  • 6
  • 56
  • 87
0

I had a similar problem and it was because the last quote was getting escaped out. I was using a multiline string with a terminating "\" to eat up the end of line character.

[ string appendFormat:@"\n\
      vec4 position;\n\
      vec4 inputTextureCoordinate;\n\" ];

After I removed the mistaken "\" that ate up the last quote it worked.

nishant
  • 736
  • 1
  • 12
  • 22