我已根据以下内容创建了一个模型,其中包含一个用于性别的自定义枚举
enum Gender :String, Codable {
case male = "Male"
case female = "Female"
}
class Person : Codable {
var name : String?
var gender : Gender?
convenience init(name : String, gender : Gender) {
self.init()
self.name = name
self.gender = gender
}
enum CodingKeys: String, CodingKey {
case name = "name"
case gender = "gender"
}
}
以下是来自API的json数据
[
{
"name": "name1",
"gender": "Male"
},
{
"name": "name2",
"gender": "Male"
}
]
在成功响应块中,如果我使用下面的代码解析数据,它可以正常工作
do {
let list = try JSONDecoder().decode([Person].self, from: data)
print("list \(list)")
}
catch {
print("error \(error)")
}
现在问题是在响应api中,如果在所有小写like "gender": "male"
中提供了性别,则解析不起作用并给出以下错误。
error dataCorrupted(Swift.DecodingError.Context(codingPath:
[_JSONKey(stringValue: "Index 1", intValue: 1), CodingKeys(stringValue:
"gender", intValue: nil)], debugDescription: "Cannot initialize Gender from
invalid String value male", underlyingError: nil))
我想要下面的任何一个解决方案
我还不想覆盖编码器和解码器方法,因为我在实际项目中有很多参数。我已经意识到了这一点。
答案 0 :(得分:1)
您可以通过覆盖init(from decoder:)
...
Person
来执行此操作
enum Gender :String, Codable {
case male, female // Note that I removed the uppercase string values
}
struct Person : Codable {
var name : String?
var gender : Gender?
enum CodingKeys: String, CodingKey {
case name, gender // No need for string values if they match the enum cases
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decodeIfPresent(String.self, forKey: .name) // decodeIfPresent as name is optional
let genderString = try container.decode(String.self, forKey: .gender)
gender = Gender(rawValue: genderString.lowercased()) // This is why I removed the uppercase String values
}
}