小编Jal*_*lem的帖子

"在Swift中解码JSON时,无法读取数据,因为它丢失了"错误

我收到以下错误:

由于缺少数据,因此无法读取数据.

当我运行以下代码时:

struct Indicator: Decodable {
    let section: String
    let key: Int
    let indicator: Int
    let threshold: Int
}
    var indicators = [Indicator]()

    do {
        if let file = Bundle.main.url(forResource: "indicators", withExtension: "json") {
            indicators = try JSONDecoder().decode([Indicator].self, from: try Data(contentsOf: file))
        }
    } catch {
        print(error.localizedDescription)
    }
Run Code Online (Sandbox Code Playgroud)

这些都是功能,但为了清楚起见,我删除了它们.我有一个代码块,它在一个不同的文件中非常相似(我从那里复制了这些代码并且基本上改变了名称)所以我不确定它为什么会发生.json文件是有效的json并且正确设置了它的目标.

谢谢

json ios swift

27
推荐指数
6
解决办法
1万
查看次数

标签 统计

ios ×1

json ×1

swift ×1