首页 > 解决方案 > Swift - 使用大 JSON 使 EXC_BAD_ACCESS JSONDecoder 崩溃

问题描述

我坚持快速解码 JSON。

我在一个带有 10 个字段的 JSON 的操场上得到了以下代码。当我尝试解码数据时,出现以下错误

error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

但是,如果我取出例如“第九”和“第十”或任何其他字段中的 2 个,则似乎不会发生此错误,因此结构中仅保留 8 个字段。

是否有只能解码 8 个字段的限制?我错过了什么吗?我能做些什么来克服这个问题吗?

我的代码片段:

let decoder = JSONDecoder()
struct Positions: Codable {
    let first : String
    let second: String
    let third: String
    let forth: String
    let fith: String
    let sixth: String
    let seventh: String
    let eigth: String
    let nineth: String
    let tenth: String
}

var positions = """
    {
    "first" : "first",
    "second": "second",
    "third": "third",
    "forth": "forth",
    "fith": "fith",
    "sixth": "sixth",
    "seventh": "seventh",
    "eigth": "eigth",
    "nineth": "nineth",
    "tenth": "tenth"
    }
""".data(using: .utf8)

    let result = try decoder.decode(Positions.self, from: positions!)
    print("tr \(result)")

标签: jsonswiftjsondecoder

解决方案


推荐阅读