Ramsel Ramsel - 1 year ago 103
Objective-C Question

'Undeclared Identifier' error with defined constants

My defined constants are giving me 'undeclared identifier' issues. I have them in a

file that I am including in my .pch file. I thought it might be something with my .pch file, however, if I delete it from there and try
it in one of the classes where I need one of the constants, then I still get an 'undeclared identifier' error.

If I take each
line and put them at the top of the .m class file directly, they work. So my syntax is correct.

So it's something with the .h file itself, but I have no idea what.

// Constants.h

// Sliding Drawer
#define kOffscreenX 320 // X coord when Sliding Drawer is offscreen
#define kVisibleX 40 // X coord when Sliding Drawer is visible

// WordlistButton
#define kNumScores 3

// Fonts
#define kMessageFontSize 14
#define kScoreFontSize 10

Answer Source

It's impossible to see the error only from this piece of code. Preprocessor tends to create very messy things, especially when there are circular imports involved.

You can try to delete the current compiled version of the header, note it's not in the derived data folder, it's in XCode's cache (see Project -> Build Setttings -> Precompiled Headers Cache Path).

However, if you have tried to import Constants.h directly and it didn't work, the problem may be somewhere else.

Are you sure there is only 1 file called Constants.h? Note you should use a prefix for your files (e.g. SAConstants.h if Smooth Almonds is your name) to avoid collision with Apple's headers or headers of the libraries you are using.

If you import the header directly, go to the .m file and tap on Product -> Generate Output -> Preprocessed File and find Constants.h import in it. Is it your header?

By the way, there is a nice article about avoiding this kind of things in precompiled headers http://qualitycoding.org/precompiled-headers/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download