简体   繁体   中英

How do I access a preprocessor macro from code in Xcode using Swift?

How do I access a preprocessor macro in Xcode from Swift code for a string I've defined? Other stack overflow 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:

预编译器宏未在预编译头中使用

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.

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:

构建设置选项卡中的活动编译条件

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