我想從主包中的文件加載數據。當我使用此代碼無法轉換'String?'類型的值到預期的參數類型'URL'
let path = Bundle.main.path(forResource: "abc", ofType: "txt")
let dataTwo = try! Data(contentsOf: path)\\ error here
我也試圖將字符串轉換爲URL
let dataTwo = try! Data(contentsOf: URL(string: "file://\(path)")!)
但執行後正在此
fatal error: unexpectedly found nil while unwrapping an Optional value
您確定您要加載的文件包含在您的包中嗎?將它們放在項目文件夾中是不夠的,您必須指示Xcode將它們複製到包中。 –
我該死的確定它只在那裏。我也檢查了生成階段的複製包資源 –
您可以檢入生成的包以查看文件是否真的存在... –