如何访问用户定义的Xcode构建设置?

时间:2023-01-24 18:26:03

If I added a user-defined setting in my build configuration, how can I read that setting in my Objective-C code?

如果我在构建配置中添加了用户定义的设置,如何在Objective-C代码中读取该设置?

I have two files in my project, debug.plist and release.plist. I want my MainApp.m file to read one of these files based on which build configuration is running. I set up a user-defined setting named "filename" in both the Debug and Release configurations to point to the appropriate file. But I don't know how my MainApp.m file can read the filename variable from the current running configuration.

我的项目中有两个文件,debug.plist和release.plist。我希望我的MainApp.m文件根据正在运行的构建配置读取其中一个文件。我在Debug和Release配置中设置了一个名为“filename”的用户定义设置,指向相应的文件。但我不知道我的MainApp.m文件如何从当前运行的配置中读取filename变量。

5 个解决方案

#1


35  

Here's what I did, I'm not 100% sure if this is what you're after:

这就是我所做的,我不是百分之百确定这是你所追求的:

  1. Go into the build Settings panel and choose the gear icon in the bottom left: add User-Defined Setting
  2. 进入构建设置面板,然后选择左下角的齿轮图标:添加用户定义的设置
  3. Create your user defined setting, for example:

    创建用户定义的设置,例如:

    MY_LANG -> en_us
    
  4. Then, in the Preprocessor Macro's setting, you can reference that value:

    然后,在预处理器宏的设置中,您可以引用该值:

    LANGCODE="$(MY_LANG)"
    

Now you can refer to LANGCODE in all your source files, and it will be whatever you filled out in your custom build setting. I realize that there's a level of indirection here, but that is intentional in my case: my XCode project contains a bunch of different targets/configurations with their own preprocessor macro's. I don't want to have to go into all of those, just to change the language code. In fact, I define the language code on the project level. I also use MY_LANG in a couple scripts, so just a preprocessor macro wouldn't do. There may be a smarter way, but this works for me.

现在,您可以在所有源文件中引用LANGCODE,它将是您在自定义构建设置中填写的任何内容。我意识到这里存在一定程度的间接,但在我的情况下这是故意的:我的XCode项目包含一堆具有自己的预处理器宏的不同目标/配置。我不想进入所有这些,只是为了更改语言代码。实际上,我在项目级别定义了语言代码。我也在几个脚本中使用MY_LANG,所以只是一个预处理器宏不会。可能有一种更聪明的方式,但这对我有用。

#2


8  

Your code can't read arbitrary build settings. You need to use preprocessor macros.

您的代码无法读取任意构建设置。您需要使用预处理器宏。

EDIT: For example, in the target settings for the Debug configuration, you could add DEBUGGING=1 in the Preprocessor Macros build setting, and not define DEBUGGING in the Release configuration. Then in your source code you could do things like:

编辑:例如,在Debug配置的目标设置中,您可以在预处理器宏构建设置中添加DEBUGGING = 1,而不在Release配置中定义DEBUGGING。然后在您的源代码中,您可以执行以下操作:

#if DEBUGGING
  use this file
#else
  use the other one
#endif

#3


6  

I tried zmippie suggestion but it didn't work for me.

我尝试了zmippie建议,但它对我不起作用。

I got it working with this:

我得到了它:

${MY_LANG}

#4


6  

You can access your user-defined build setting at run-time (as suggested in a comment by @JWWalker)

您可以在运行时访问用户定义的构建设置(如@JWWalker的评论中所示)

  1. Add an entry to your Info.plist file, and set it to your User-defined Build Setting

    在Info.plist文件中添加一个条目,并将其设置为用户定义的构建设置

    MySetting -> ${MYSETTING}
    
  2. Read its value from code

    从代码中读取它的值

    [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MySetting"];
    

#5


0  

In case anyone else is still stuck looking for how to do preprocessor macros, look for the Apple LLVM - Preprocessing section in Build Settings. Under it, you will see a section called Preprocessor Macros.

如果其他人仍然在寻找如何处理预处理器宏,请在Build Settings中查找Apple LLVM - 预处理部分。在它下面,您将看到一个名为Preprocessor Macros的部分。

This is where by default, Xcode inserts the DEBUG=1 macro for the debug build configuration.

这是默认情况下,Xcode为调试构建配置插入DEBUG = 1宏。

You can add your own here, and give them different values for debug, release and any custom build configs you may have.

您可以在此处添加自己的值,并为调试,发布和您可能拥有的任何自定义构建配置提供不同的值。

To add one, double-click on the current value list for the config you want, and it'll display a nice little editor with one macro on each line. Just add your own macro name, and give it a value the same way the DEBUG one is done.

要添加一个,请双击所需配置的当前值列表,它将显示一个漂亮的小编辑器,每行有一个宏。只需添加您自己的宏名称,并以与DEBUG完成相同的方式为其赋值。

These can be checked during the preprocessor build phase using #if, #ifdef etc. to provide conditional code or values.

可以在预处理器构建阶段使用#if,#ifdef等来检查这些,以提供条件代码或值。

Hope that helps.

希望有所帮助。

#1


35  

Here's what I did, I'm not 100% sure if this is what you're after:

这就是我所做的,我不是百分之百确定这是你所追求的:

  1. Go into the build Settings panel and choose the gear icon in the bottom left: add User-Defined Setting
  2. 进入构建设置面板,然后选择左下角的齿轮图标:添加用户定义的设置
  3. Create your user defined setting, for example:

    创建用户定义的设置,例如:

    MY_LANG -> en_us
    
  4. Then, in the Preprocessor Macro's setting, you can reference that value:

    然后,在预处理器宏的设置中,您可以引用该值:

    LANGCODE="$(MY_LANG)"
    

Now you can refer to LANGCODE in all your source files, and it will be whatever you filled out in your custom build setting. I realize that there's a level of indirection here, but that is intentional in my case: my XCode project contains a bunch of different targets/configurations with their own preprocessor macro's. I don't want to have to go into all of those, just to change the language code. In fact, I define the language code on the project level. I also use MY_LANG in a couple scripts, so just a preprocessor macro wouldn't do. There may be a smarter way, but this works for me.

现在,您可以在所有源文件中引用LANGCODE,它将是您在自定义构建设置中填写的任何内容。我意识到这里存在一定程度的间接,但在我的情况下这是故意的:我的XCode项目包含一堆具有自己的预处理器宏的不同目标/配置。我不想进入所有这些,只是为了更改语言代码。实际上,我在项目级别定义了语言代码。我也在几个脚本中使用MY_LANG,所以只是一个预处理器宏不会。可能有一种更聪明的方式,但这对我有用。

#2


8  

Your code can't read arbitrary build settings. You need to use preprocessor macros.

您的代码无法读取任意构建设置。您需要使用预处理器宏。

EDIT: For example, in the target settings for the Debug configuration, you could add DEBUGGING=1 in the Preprocessor Macros build setting, and not define DEBUGGING in the Release configuration. Then in your source code you could do things like:

编辑:例如,在Debug配置的目标设置中,您可以在预处理器宏构建设置中添加DEBUGGING = 1,而不在Release配置中定义DEBUGGING。然后在您的源代码中,您可以执行以下操作:

#if DEBUGGING
  use this file
#else
  use the other one
#endif

#3


6  

I tried zmippie suggestion but it didn't work for me.

我尝试了zmippie建议,但它对我不起作用。

I got it working with this:

我得到了它:

${MY_LANG}

#4


6  

You can access your user-defined build setting at run-time (as suggested in a comment by @JWWalker)

您可以在运行时访问用户定义的构建设置(如@JWWalker的评论中所示)

  1. Add an entry to your Info.plist file, and set it to your User-defined Build Setting

    在Info.plist文件中添加一个条目,并将其设置为用户定义的构建设置

    MySetting -> ${MYSETTING}
    
  2. Read its value from code

    从代码中读取它的值

    [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MySetting"];
    

#5


0  

In case anyone else is still stuck looking for how to do preprocessor macros, look for the Apple LLVM - Preprocessing section in Build Settings. Under it, you will see a section called Preprocessor Macros.

如果其他人仍然在寻找如何处理预处理器宏,请在Build Settings中查找Apple LLVM - 预处理部分。在它下面,您将看到一个名为Preprocessor Macros的部分。

This is where by default, Xcode inserts the DEBUG=1 macro for the debug build configuration.

这是默认情况下,Xcode为调试构建配置插入DEBUG = 1宏。

You can add your own here, and give them different values for debug, release and any custom build configs you may have.

您可以在此处添加自己的值,并为调试,发布和您可能拥有的任何自定义构建配置提供不同的值。

To add one, double-click on the current value list for the config you want, and it'll display a nice little editor with one macro on each line. Just add your own macro name, and give it a value the same way the DEBUG one is done.

要添加一个,请双击所需配置的当前值列表,它将显示一个漂亮的小编辑器,每行有一个宏。只需添加您自己的宏名称,并以与DEBUG完成相同的方式为其赋值。

These can be checked during the preprocessor build phase using #if, #ifdef etc. to provide conditional code or values.

可以在预处理器构建阶段使用#if,#ifdef等来检查这些,以提供条件代码或值。

Hope that helps.

希望有所帮助。