2017-05-30 51 views
0

在macOS应用程序中,我使用此代码在Application Support文件夹中创建一个目录。如何在Swift中以编程方式获取应用程序ID?

let directoryURL = appSupportURL.appendingPathComponent("com.myCompany.myApp").appendingPathComponent("Documents") 

如何串com.myCompany.myApp斯威夫特编程获得?

我看到了这个问题,但我不知道如何使用它在我的MacOS斯威夫特应用程序:Access App Identifier Prefix programmatically

+1

如果你使用沙盒,那么这是一个没有问题的问题。您的应用程序支持文件夹是您自己的。 – matt

回答

4
if let bundleIdentifier = Bundle.main.bundleIdentifier{ 
    appSupportURL.appendingPathComponent("\(bundleIdentifier)").appendingPathComponent("Documents") 
} 
+1

小解释:属性bundleIdentifier是可选的,因此您必须安全地解开该值,然后您将不会被要求提供任何感叹号:) –

+2

为什么不是您在答案中的解释?为什么把它放在评论中? – rmaddy

1

这是很简单的获取应用程序ID:

let bundleIdentifier = Bundle.main.bundleIdentifier 
    appSupportURL.appendingPathComponent("\(bundleIdentifier)").appendingPathComponent("Documents") 

一捆绑包标识符是分配给捆绑的Info.plist文件中的CFBundleIdentifier键的字符串。此字符串通常使用反向DNS表示法进行格式化,以防止名称空间与其他公司的开发人员发生冲突。例如,Apple的Finder插件可能会使用字符串com.apple.Finder.MyGetInfoPlugin作为其包标识符。而不是指针传递到一个Bundle对象在你的代码,客户需要一个包的引用可以简单地使用包标识符来检索它

详情&其他操作的详细信息,请

https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html

相关问题