设置用户特定的预处理器宏

设置用户特定的预处理器宏

本文介绍了为 Xcode 设置用户特定的预处理器宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够拥有特定的代码块,例如 #ifdef SOME_VARIABLE 并且如果项目是在特定用户的机器上构建,则变量的值将在项目构建时填充.

I'd like to be able to have specific code blocks such as #ifdef SOME_VARIABLE and the value of variable would be filled at project's build time if the project is being built on a specific user's machine.

这可能吗?

推荐答案

您在预处理器宏"构建设置中设置值.在构建设置中设置 "SOME_VARIABLE=${USER}" 等效于代码中的 #define SOME_VARIABLE "jappleseed".

You set the value in the "Preprocessor Macros" Build Settings. Setting "SOME_VARIABLE=${USER}" in the build settings is equivalent to #define SOME_VARIABLE "jappleseed" in your code.

然后在你的代码中你可以这样做:

Then in your code you can do this:

#define jappleseed 1
#define sjobs      2

#if DEV_USER == jappleseed
    NSLog(@"Hi Jhonny");
#elif DEV_USER == sjobs
    NSLog(@"Hi Steve");
#endif

注意:这是一个人为的例子,如果你真的想要在你的代码中使用字符串jappleseed",你应该使用 Info.plist 而不是 #define

Note: This is a contrived example if you really want the string "jappleseed" for use in your code you should be using an Info.plist and not #define

这篇关于为 Xcode 设置用户特定的预处理器宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 06:00