ios 在 Swift 中以编程方式获取包标识符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25897086/
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
Obtain bundle identifier programmatically in Swift?
提问by User
How can I get the bundle ID in Swift?
如何在 Swift 中获取包 ID?
Objective-C version:
Objective-C 版本:
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
回答by Sebastian
Try this:
尝试这个:
let bundleID = NSBundle.mainBundle().bundleIdentifier
Swift 3+:
斯威夫特 3+:
let bundleID = Bundle.main.bundleIdentifier
回答by User
It's pretty much the same thing in Swift except the class and method names have been shortened:
除了类名和方法名被缩短了之外,这在 Swift 中几乎是一样的:
let bundleIdentifier = Bundle.main.bundleIdentifier // return type is String?
回答by Aks
If you are trying to get it programmatically , you can use below line of code :
如果您尝试以编程方式获取它,则可以使用以下代码行:
Objective-C:
目标-C:
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
Swift 3.0:
斯威夫特 3.0:
let bundleIdentifier = Bundle.main.bundleIdentifier
Updated for latest swift It will work for both iOS and Mac apps.
更新为最新的 swift 它适用于 iOS 和 Mac 应用程序。
For More Info, Check here :
欲了解更多信息,请点击此处:
Apple Docs: https://developer.apple.com/documentation/foundation/bundle#//apple_ref/occ/instm/NSBundle/bundleIdentifier
苹果文档:https: //developer.apple.com/documentation/foundation/bundle#//apple_ref/occ/instm/NSBundle/bundleIdentifier