简体   繁体   中英

Xcode 5 weird errors

Something strange is going on with my Xcode 5. All of a sudden I'm getting Undeclared Identifier errors for all the values in my Constants.h file, which is imported in my Prefix.pch file.

Two things are weird here:

  1. This hasn't happened before.
  2. When I do build and run, the build succeeds and the app runs with no problems.

I tried restarting Xcode and the simulator, and even restarting the whole machine. No luck.

What's going on? How can I get rid of these false errors?

EDIT following rmaddy's request. The error is Use of undeclared identifier kOffsetFromTop (for example, there are other similar errors with different constants.)

I don't really want to post my entire constants file, but the constant in question is defined like this:

static int const kOffsetFromTop = 20;

When this happens I normally do the following

  • Comment out the import from the .pch
  • Clean all ⌘⌥⇧K
  • Delete derived data
  • Build

Then uncomment the import from the .pch and build again. I'm not sure which step is actually sorting the issue but this normally gets me going again.

Multiple points here :

  1. You have a warning, not an error. A warning is just that, it warns you but does not prevent the code to compile. That is just to warning you that something is odd or unexpected, or to tell you that what you wrote may not be actually what you were intended, because the compiler finds it odd or not standard.
  2. You didn't have the warning before probably because it wasn't activated by default in previous versions of Xcode. The latest version of Xcode5 activates more warnings (which is a good thing, as it warns you about more things that could go wrong in your code and encourage you to fix them), hence this new one you have but didn't have before.
  3. As I understand what you describe, tour usage of a constant is incorrect (and that's probably why Xcode emits a warning).

The correct way to declare a constant that you want to be accessible from multiple files is to:

  • Declare it (define its existence and type) in a header (or in your pch) like this: extern <type> const <name>;
  • And define it (give it a value) only in one implementation file (like your main.m, your AppDelegate.m, some Constants.m file, whatever) like this: <type> const <name> = <value>

Some details and reminders about constants declaration (also valid in standard C)

  • You use static <type> const <name> = <value>; in an implementation file only, when the constant is local to the file and does not need to be used by other files . In that case, you declare it typically in the .m file in which you will use it, and other files won't have access to it (which is quite what the static keyword means, actually (making the constant attached/local to the file).
  • In that matter, you should never declare a constant that way in a header file (and especially not in your .pch file), because header (and pre-compiled header) files will be included multiple times. If you do that, this would declare as many independent constants as the number of implementation files you include your headers into (this has evil side effects especially for pointers/objects, for exemple declaring an NSString* const that way -- for, say, using it as a notification name of error domain -- will create multiple string constants, with the same value but different addresses, which will probably not behave like you will expect)
  • When you need to declare a constant that you need to use / be accessible from multiple implementation files, so declaring this constant in a header file so that it is known to all implementation fiels that includes this header, you need to only declare it in the header, and not making it static (at this would run against the purpose of having the same constant for all files instead of multiple independent instances of the constant) but instead indicate extern to let the compiler know that its definition (value) is set elsewhere. Hence the solution given above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM