![](/img/trans.png)
[英]Bundle.main.path(forResource:ofType:inDirectory:) returns nil when directory and filename are correct
[英]Bundle.main.path(forResource:ofType:inDirectory:) returns nil
盡量不要笑或哭——我在 20 年后才重新開始編碼......
我花了 4 個多小時查看參考資料並嘗試使用代碼片段來獲取 Bundle.main.path 以打開我的文本文件,以便我可以讀取我的應用程序的數據(我的下一步是適當地解析它)。
if let filepath = Bundle.main.path(forResource: "newTest", ofType: "txt")
{
do
{
let contents = try String(contentsOfFile: filepath)
print(contents)
}
catch
{
print("Contents could not be loaded.")
}
}
else
{
print("newTest.txt not found.")
}
結果是:“找不到newTest.txt”。 無論我如何嘗試將文件拖放到項目中,在 Xcode 中創建文件或使用 File -> Add Files to... 菜單項。
問題是該文件沒有被復制到您的應用程序包中。 要解決這個問題:
斯威夫特 3.0
let fileNmae = "demo"
let path = Bundle.main.path(forResource: fileName, ofType: "txt")
do {
let content = try String(contentsOfFile:path!, encoding: String.Encoding.utf8)
print(content)
} catch {
print("nil")
}
斯威夫特 2.0
do{
if let path = NSBundle.mainBundle().pathForResource("YOURTXTFILENAME", ofType: "txt"){
let data = try String(contentsOfFile:path, encoding: NSUTF8StringEncoding)
let myStrings = data.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
print(myStrings)
}
} catch let err as NSError {
//do sth with Error
print(err)
}
輸出 :
Hello Hems
Good Morning
I m here for you dude.
Happy Coding.
這么簡單的問題要花費多少時間,真是太瘋狂了。
這里的一些答案對我有幫助,但我總是嘗試:
Bundle.main.url(forResource: selectedTitle, withExtension: "mp3")
出於某種原因,這不起作用,但獲取路徑並將其轉換為 URL 之后可以工作:
let path = Bundle.main.path(forResource: selectedTitle, ofType: "mp3")
let url = URL(fileURLWithPath: path!)
這對我很有幫助: xcode - 將文件夾結構復制到我的應用程序包中
在嵌套文件夾中創建資產時,標記“創建文件夾引用”選項。
然后像這樣找到文件的路徑:
let path = Bundle(for: type(of : self)).path(forResource: "some_folder_a/some_folder_b/response.json", ofType: nil)
同樣的問題,情況和解決方案略有不同。 我正在關注一個教程,該教程說使用以下代碼:
// Start the background music:
if let musicPath = Bundle.main.path(forResource:
"Sound/BackgroundMusic.m4a", ofType: nil) {
print("SHOULD HEAR MUSIC NOW")
let url = URL(fileURLWithPath: musicPath)
do {
musicPlayer = try AVAudioPlayer(contentsOf: url)
musicPlayer.numberOfLoops = -1
musicPlayer.prepareToPlay()
musicPlayer.play()
}
catch { print("Couldn't load music file") }
}
}
我和其他人有同樣的問題,但沒有一個解決方案解決了這個問題。 經過試驗,我所做的就是在以下調用中從路徑中刪除Sound並且一切正常:
Bundle.main.path(forResource: "BackgroundMusic.m4a", ofType: nil)
通過告訴我在路徑中包含聲音,教程似乎是錯誤的。
我將file.txt
添加到我的項目中,它會自動添加到我的項目的Copy Bundle Files
中。 對我來說,我必須從forResource
刪除擴展名並且它起作用了。
let path = Bundle.main.path(forResource: "file", ofType: "txt") // not forResource: "file.txt"
我認為您不想要inDirectory:
方法。 試試這個:
if let filepath = Bundle.main.path(forResource: "newTest", ofType: "txt") {
我的問題出在一個用 Cocoapods 創建的模塊中。 每個 pod 安裝/更新,文件(json 文件)都在項目中,但從未在 Bundle 中找到。 在 pod install/update 之后,我刪除了(刪除了引用)文件並再次添加到項目中。
始終注意添加文件的目標。
答案很簡單,不要使用 Assets.xcassets 來存儲您的音頻文件,而將您的音頻文件,即)BeTheFirstAudio.m4a(使用 QuickTime 制作)直接添加到 PROJECT NAVIGATOR(圓圈、粉紅色)文件列表中(照片 1)
警告! 在從 Finder 窗口拖動音頻文件后出現彈出窗口時,請務必選中“創建組”和“添加到目標”復選框。
之后,您的代碼應該可以播放音頻(包括來自 QuickTime 的 .m4a 擴展名)
import AVFoundation
class AudioPlayer: NSObject {
private var audioPlayer: AVAudioPlayer!
override init() {
super.init()
// get URL for the default audio
let audioURL = Bundle.main.url(forResource: "BeTheFirstAudio", withExtension: "m4a")
audioPlayer = try! AVAudioPlayer(contentsOf: audioURL!)
}
蘋果工程師應該感到羞恥的是,這個問題甚至是 StackOverflow 中的一個問題; 開發人員應該能夠將音頻文件添加到 Assets.xcassets 並且 Bundle.main.url 應該能夠找到它。 這個簡單的概念很困難,這一事實可能讓開發人員花費了大量時間。 注意@Apple
如果你使用R.swift試試這個:
我仍然遇到特別是在構建期間更改的 plist 文件的問題,並且由於我的項目使用的是 R.swift,因此我將代碼更改為
Bundle.main.path(forResource: R.file.myplistfile)
這就像一個魅力。
如果您正在運行單元測試或 ui 測試,請使用它們的包 ID。 因為他們沒有使用主包,所以你永遠不會通過調用Bundle.main.path(forResource: "newTest", ofType: "txt")
找到你的文件。
使用您的測試目標包 ID。 就我而言,我正在嘗試從 CSV 文件中讀取數據。 將TestReadFileUITests
替換為從XCTestCase
擴展的 class 名稱。
這是我運行 UI 測試用例時使用的實際捆綁包 ID。 您可以看到它不是來自主捆綁包。
(lldb) po testBundle
NSBundle </Users/zhouhaibo/Library/Developer/CoreSimulator/Devices/448D1FCB-8311-4D3F-BEEF-934052C3470F/data/Containers/Bundle/Application/905C2326-8313-410F-9DC2-9D84B9C6D757/TestReadFileUITests-Runner.app/PlugIns/TestReadFileUITests.xctest> (loaded)
(lldb) po filePath
"/Users/zhouhaibo/Library/Developer/CoreSimulator/Devices/448D1FCB-8311-4D3F-BEEF-934052C3470F/data/Containers/Bundle/Application/905C2326-8313-410F-9DC2-9D84B9C6D757/TestReadFileUITests-Runner.app/PlugIns/TestReadFileUITests.xctest/Patients_2022-09-02.csv"
let testBundle = Bundle(for: TestReadFileUITests.self)
guard let filePath = testBundle.path(forResource: fileName, ofType: fileType) else {
print("CSV file not found.")
return [:]
}
class TestReadFileUITests: XCTestCase {
func testDemo() {
let _ = DataManager.readData()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.