3

How do I access a preprocessor macro in Xcode from Swift code for a string I've defined? Other stackoverflow posts show how to create the macros, but do not show me how to access them in code sufficiently.

For example, I have defined a constant called HELLO_WORLD_STRING = "Hello World!" in Preprocessor Macros Not Used in Precompiled Headers in the build settings as in the image:

Preprocessor Macros Not Used in Precompiled Headers

How would I print that constant in the debug window, just as an example?

I actually need to use the value of the app name to set the title of a navigation bar, but I would like to take this route in order to do that.

I have tried using #if ... #endif statements, but the constant was not recognized.

9
  • What language are you using? Swift eschews the use of macros. #ifdef work, but defines, not so much. Commented Dec 6, 2018 at 8:03
  • I'm using Swift. Commented Dec 6, 2018 at 8:04
  • And I have to ask: what benefit do you think you'd get by retrieving this value from the build system, rather than setting it for your self in your application delegate, say? Commented Dec 6, 2018 at 8:05
  • As far as I can tell, it's not possible in Swift. You can get the full path of the executable with Bundle.main.executablePath. Commented Dec 6, 2018 at 8:06
  • 1
    Yeah, so Swift is valiantly trying to kill off the use of macros, as they can make code hard to reason about, being one more place one needs to look for constants. Commented Dec 6, 2018 at 8:15

1 Answer 1

4

If you want to use syntax like:

 #if DEBUG_API_CALL
    print("Start loading \(method) \(url)")
 #endif

You have to add DEBUG_API_CALL (or other your name) to Active Compilation Conditions in the build settings tab:

Active Compilation Conditions in the build settings tab

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.