xcode 如何在 Obj-C 代码中引用环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3727891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reference an environment variable inside Obj-C code
提问by highlycaffeinated
I define a path variable in Xcode source tree called "MY_SRC_DIR". I would like to get the value of this environment variable and put it in a NSString in the obj-c code. For example,
我在 Xcode 源代码树中定义了一个名为“MY_SRC_DIR”的路径变量。我想获取这个环境变量的值,并把它放在 obj-c 代码中的一个 NSString 中。例如,
-(NSString*) getSourceDir
{
return @"${MY_SRC_DIR}"; // not the right solution and this is the question
}
回答by highlycaffeinated
From http://rosettacode.org/wiki/Environment_variables#Objective-C:
来自http://rosettacode.org/wiki/Environment_variables#Objective-C:
[[NSProcessInfo processInfo] environment]
returns an NSDictionary of the current environment.
[[NSProcessInfo processInfo] environment]
返回当前环境的 NSDictionary。
For example:
例如:
[[[NSProcessInfo processInfo] environment] objectForKey:@"MY_SRC_DIR"]
回答by David Hernandez
Just expose the desired var into the Environment Variables list of your current Xcode's deployment Scheme and you'll be able to retrieve it at runtime like this:
只需将所需的变量公开到当前 Xcode 部署方案的环境变量列表中,您就可以在运行时检索它,如下所示:
NSString *buildConfiguration = [[NSProcessInfo processInfo] environment][@"BUILD_CONFIGURATION"];
It also applies to swift based projects.
它也适用于基于 swift 的项目。
Hope it helps!! :]
希望能帮助到你!!:]
回答by William Jockusch
Here is another way to do it:
这是另一种方法:
.xcconfig file:
.xcconfig 文件:
FIRST_PRESIDENT = '@"Washington, George"'
GCC_PREPROCESSOR_DEFINITIONS = MACRO_FIRST_PRESIDENT=$(FIRST_PRESIDENT)
objective C code:
目标C代码:
#ifdef FIRST_PRESIDENT
NSLog(@"FIRST_PRESIDENT is defined");
#else
NSLog(@"FIRST_PRESIDENT is NOT defined");
#endif
#ifdef MACRO_FIRST_PRESIDENT
NSLog(@"MACRO_FIRST_PRESIDENT is %@", MACRO_FIRST_PRESIDENT);
#else
NSLog(@"MACRO_FIRST_PRESIDENT is undefined, sorry!");
#endif
Console output -- I've stripped out the garbage from NSLog:
控制台输出——我已经从 NSLog 中去除了垃圾:
FIRST_PRESIDENT is NOT defined
MACRO_FIRST_PRESIDENT is Washington, George
回答by geowar
The only way I've found to get a build time environment variable as a string is to put it in an dictionary element like this:
我发现将构建时环境变量作为字符串获取的唯一方法是将其放入字典元素中,如下所示:
<key>Product Name</key>
<string>$PRODUCT_NAME</string>
and then retrieve it like this:
然后像这样检索它:
NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSString* productName = infoDict[@"Product Name"];
NSLog(@"Product Name: %@", productName);
回答by bobwaycott
The best answer to this question is the accepted answer on this question.
这个问题的最佳答案是这个问题的公认答案。
You'll get the most mileage, and won't need any special methods to get the value you're searching for as long as you import the file into whatever .h/.m file is going to consume said value.
您将获得最大的收益,并且不需要任何特殊方法来获取您正在搜索的值,只要您将文件导入到将消耗所述值的任何 .h/.m 文件中即可。