0

在过去的 3-4 个小时里,我一直在尝试让这个愚蠢的东西正确解码这个枚举,现在对此感到非常沮丧!我有一个json从 API 返回的字符串,如下所示:

[
  {
    "contractType": 0
  }
]

我正在尝试将其映射到一个名为ContractType. 这是我的整个ContractType枚举

enum ContractType: Int {
    case scavenger = 0
}

这是我的扩展,我试图使其符合Codable协议。

extension ContractType: Codable {
    enum Key: Int, CodingKey {
        case rawValue
    }

    enum CodingError: Error {
        case unknownValue
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: Key.self)
        let rawValue = try? container.decode(Int.self, forKey: .rawValue)

        switch rawValue {
        case 0:
            self = .scavenger
        default:
            throw CodingError.unknownValue
        }
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: Key.self)
        switch self {
        case .scavenger:
            try container.encode(0, forKey: .rawValue)
        }
    }
}

我到底做错了什么!?任何帮助将不胜感激!

4

2 回答 2

2

要解析上述JSON 响应Codable模型应该是,

struct Response: Codable {
    let contractType: ContractType
}

enum ContractType: Int, Codable {
    case scavenger = 0
}

现在,像这样解析JSON , data

do {
    let response = try JSONDecoder().decode([Response].self, from: data)
    print(response)
} catch {
    print(error)
}

无需显式实现init(from:)and encode(to:)。它将由编译器自动处理。

于 2020-05-08T06:17:58.527 回答
1

这就是你所需要的。

struct Contract: Codable {
    var contractType: ContractType

    enum ContractType: Int, Codable {
        case scavenger
    }
}

do {
    let contract = try JSONDecoder().decode([Contract].self, from: json)
    print(contract.contractType)
} catch {
    print(error)
}
于 2020-05-08T06:34:46.697 回答