我需要檢索通過在項目目錄(又名mainBundle)獲取資源的URL
所以開始的路徑包含在我的Xcode項目資源的URL,如果,如果我./snd指定路徑/archer/acknowledge1.wav,我需要從中創建一個URL。
我知道我可以使用NSURL(fileURLWithPath:)
作爲系統目錄,但我不知道如何直接從包中執行此操作。
我需要檢索通過在項目目錄(又名mainBundle)獲取資源的URL
所以開始的路徑包含在我的Xcode項目資源的URL,如果,如果我./snd指定路徑/archer/acknowledge1.wav,我需要從中創建一個URL。
我知道我可以使用NSURL(fileURLWithPath:)
作爲系統目錄,但我不知道如何直接從包中執行此操作。
你會使用束國土資源URL方法
[[NSBundle mainBundle] URLForResource:@"acknowledge1"
withExtension:@"wav"
subdirectory:@"snd/archer"];
NSBundle.mainBundle().URLForResource("acknowledge1", withExtension:"wav" subdirectory:"snd/archer")
在最新一期斯威夫特:
Bundle.main.url(forResource: "acknowledge1", withExtension:"wav")
隨着斯威夫特3,得到的答案是:
Bundle.main.url(forResource: "acknowledge1", withExtension: "wav" subdirectory: "snd/archer")
Swift 2.3 y你不得不使用這個解開:
if let resourceUrl = NSBundle.mainBundle().URLForResource("acknowledge1", withExtension: "wav", subdirectory:"snd/archer") {
if NSFileManager.defaultManager().fileExistsAtPath(resourceUrl.path!) {
print("file found")
//do stuff
}
}
請修復您的答案。 '子目錄'之前需要逗號。這對於Swift 4(也許Swift 3)來說是個很好的答案, –