objective-c 如何获得目标名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18783055/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:58:00  来源:igfitidea点击:

How to get Target name?

iphoneobjective-cxcodebuild-process

提问by Iducool

We know that Xcode maintains environment variable of ${TARGET_NAME}but how to access this variable in objective-C code ?

我们知道 Xcode 维护环境变量,${TARGET_NAME}但是如何在 Objective-C 代码中访问这个变量?

What I have tried ?
I have added "TARGET_NAME=${TARGET_NAME}"this in Preprocessor macros section of Build Settings. But now I am not sure how to use this variable "TARGET_NAME"as a string in objective-C code.

我试过什么?
"TARGET_NAME=${TARGET_NAME}"在构建设置的预处理器宏部分添加了这个。但是现在我不确定如何"TARGET_NAME"在 Objective-C 代码中将此变量用作字符串。

In my case product name and target name are different so, no chance to use that.

在我的情况下,产品名称和目标名称不同,所以没有机会使用它。

I tried to access using

我试图访问使用

#ifdef TARGET_NAME
 NSLog(@"TargetIdentifier %@",TARGET_NAME);
#endif

This code is giving error like "Use of undeclared identifier 'myapptargetname'"

此代码给出错误,如“使用未声明的标识符‘myapptargetname’”

回答by Sergey Demchenko

You can add "TargetName" key to your Info.plist file:

您可以将“TargetName”键添加到您的 Info.plist 文件中:

enter image description here

在此处输入图片说明

Then you can access it (swift code):

然后你可以访问它(快速代码):

var plistFileName = NSBundle.mainBundle().infoDictionary?["TargetName"] as String

回答by Jonny Vu

NSLog(@"Target name: %@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]);

Hope to help you!

希望能帮到你!

Edited: "CFBundleName"thanks Max and Daniel Bo for your commend

编辑:“CFBundleName”感谢 Max 和 Daniel Bo 的推荐

回答by Lawliet

Swift 4, Xcode 9+

斯威夫特 4,Xcode 9+

Bundle name:

套装名称:

Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String ?? ""

Bundle display name:

捆绑显示名称:

Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? ""

Swift 3, Xcode 8+

斯威夫特 3,Xcode 8+

let targetName = Bundle.main.infoDictionary?["CFBundleName"] as? String ?? ""

回答by Andreas -he-her-

I must credit the original answer by vk.edward.li, as this answer only merely expands on it.

我必须归功于vk.edward.li 的原始答案,因为这个答案只是对其进行了扩展。

All the build setting macros can easily be used in source code as preprocessor macros too. The problem is that they're not set in advance; you have to specify which ones you get access to, yourself.

所有构建设置宏也可以在源代码中轻松用作预处理器宏。问题是它们没有提前设置;您必须自己指定可以访问哪些。

enter image description here

在此处输入图片说明

I've already added three extra preprocessor macros. Simply click the +, and add this: TARGET_NAME=\""$TARGET_NAME"\", and you've granted yourself access to one more build setting macro.

我已经添加了三个额外的预处理器宏。只需单击+,然后添加:TARGET_NAME=\""$TARGET_NAME"\",您就已授予自己对另一个构建设置宏的访问权限。

Project settings ––> Build Settings ––> Apple Clang - Preprocessing[Preprocessor Macros]. Note that if you try to insert them for both compilation modes at once, the DEBUG=1macro will get deleted, but it can as easily be added right back afterwards.

Project settings ––> Build Settings ––> Apple Clang - Preprocessing[Preprocessor Macros]. 请注意,如果您尝试同时为两种编译模式插入它们,DEBUG=1宏将被删除,但之后可以轻松地将其添加回来。

It's also worth noting that the macro values should be set differently for different languages: Objective-Cand C++/Swift, etc. This is due to the NSStringprefix used in Objective-C, so for that language, it should be MACRO=@\""$MACRO"\"instead of MACRO=\""$MACRO"\".

还值得注意的是,对于不同的语言,宏值应该设置不同的:Objective-CC++/Swift等。这是由于在 中NSString使用的前缀Objective-C,因此对于该语言,它应该MACRO=@\""$MACRO"\"代替MACRO=\""$MACRO"\"

回答by Abo3atef

In Xcode 7.3.1

在 Xcode 7.3.1 中

if let targetName = NSBundle.mainBundle().infoDictionary?["CFBundleName"] as? String{
    print(targetName)
}