2017-05-30 101 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

相關問題