intsig171 / SmartCodable

SmartCodable is a data parsing library based on Codable. It is simple to use, with robust compatibility being one of its main features. SmartCodable 是基于Codable实现的数据解析库。简单易用,强悍的兼容性是SmartCodable的主要特点。 表层API和功能几乎和HandyJSON一致,支持快速的迁移。
https://smart-codable.vercel.app
MIT License
417 stars 43 forks source link

class 解析继承 #47

Closed burning-git closed 1 month ago

burning-git commented 1 month ago
class BaseModel: SmartCodable {
    var name: String = ""
    var age: Int = 0

    private enum BaseCodingKeys: CodingKey {
        case name
        case age
    }

    required init(from decoder: Decoder) throws {
        let container: KeyedDecodingContainer<BaseCodingKeys> = try decoder.container(keyedBy: BaseCodingKeys.self)
        self.name = try container.decode(String.self, forKey: .name)
        self.age = try container.decode(Int.self, forKey: .age)
    }

    required init() { }
}

请问下 使用 class 的话,只能每个属性去解析吗?

intsig171 commented 1 month ago

是的,目前没好办法。 可以手动解析。或者使用@SmartFlat 95efeecceb1f541eb7ef04306e8c49c4

intsig171 commented 1 month ago

目前只有这两种方式。 建议减少继承的使用。 image image