ExCodable icon indicating copy to clipboard operation
ExCodable copied to clipboard

测试了一下 不支持类型嵌套, 类型兼容也有问题 声明为int类型的属性 服务端返回的是String 直接解析失败,

Open zhangjiang1203 opened this issue 8 months ago • 1 comments

有如下接口的json字符串 需要解析为下面的模型数据,测试多次threeDayForecast一直解析失败,是不是该库不支持嵌套类型的解析,

 fileprivate let jsonStr = """
{
    "username": "yuhanle",
    "age": 18,
    "weight": 65.4,
    "sex": 1,
    "location": "Toronto, Canada",
    "three_day_forecast": [
        {
            "conditions": "Partly cloudy",
            "day": "Monday",
            "temperature": 20
        },
        {
            "conditions": "Showers",
            "day": "Tuesday",
            "temperature": 22
        },
        {
            "conditions": "Sunny",
            "day": "Wednesday",
            "temperature": 28
        }
    ]
}
"""
//MARK: Excodable
struct ZJExcodableModel: Equatable {
    static func == (lhs: ZJExcodableModel, rhs: ZJExcodableModel) -> Bool {
        lhs.username == rhs.username
    }
    
    var username: String?
    var age: Int?
    var weight: Double?
    var sex: Int?
    var location: String?
    var threeDayForecast: [ZJThreeDayExcodableModel]?
}

extension ZJExcodableModel: ExCodable {
    static var keyMapping: [KeyMap<Self>] = [
        KeyMap(\.username, to: "username","title"),
        KeyMap(\.age, to: "age"),
        KeyMap(\.weight, to: "weight"),
        KeyMap(\.sex, to: "sex"),
        KeyMap(\.location, to: "location"),
        KeyMap(\.threeDayForecast, to: "three_day_forecast")
    ]
}


struct ZJThreeDayExcodableModel {
    
    var conditions: String?
    var day: String?
    var temperature: Int?
}

extension ZJThreeDayExcodableModel: ExCodable {
    static var keyMapping: [KeyMap<Self>] = [
        KeyMap(\.conditions, to: "conditions"),
        KeyMap(\.day, to: "day"),
        KeyMap(\.temperature, to: "temperature")
    ]
    
}

zhangjiang1203 avatar Jun 26 '24 09:06 zhangjiang1203