这是我尝试做的:
if let path = Bundle.main.path(forResource: "file_1", ofType: "json"), let url = URL(string: path) {
do {
let data = try Data(contentsOf: url, options: Data.ReadingOptions.mappedIfSafe)
} catch {
print("error: \(error)")
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的是:
错误:错误域= NSCocoaErrorDomain代码= 256“无法打开文件“ file_1.json”。” UserInfo = {NSURL = / var / containers / Bundle / Application / 195E2A54-5B47-44E2-92AC-25B4074A43D1 / Parser.app / file_1.json}
如何解决此错误?
该文件存在于我的项目目录中:
尝试
let url = URL(fileURLWithPath: path)
Run Code Online (Sandbox Code Playgroud)
代替
let url = URL(string: path)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1368 次 |
最近记录: |