我正在为我的一个项目https://developer.apple.com/tutorials/swiftui/creating-and-combining-views使用地标教程
我很难解码.json文件它显示了这一点Fatal error: Couldn't find landmarkData.json in main bundle.: file
我检查了我的Build Phase和Copy Bundle Resources,文件出现在那里。
let landmarkData: [Landmark] = load("landmarkData.json")
func load<T: Decodable>(_ filename: String) -> T {
let data: Data
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {
fatalError("Couldn't find \(filename) in main bundle.") //New Error here Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
}
do {
data = try Data(contentsOf: file)
} catch {
fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
}
do {
let decoder = JSONDecoder()
return try decoder.decode(T.self, from: data)
} catch {
fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
}
}
1条答案
按热度按时间sycxhyv71#
我又一次将landMark.json文件拖放到树中的Resources文件夹中,并且成功了。希望能帮上忙。