Codable : does not conform to protocol 'Decodable'

坚强是说给别人听的谎言 提交于 2019-12-12 10:53:55

问题


Not able to figure why my class does not conform to Codable Please not that in my case I do not need to implement the methods encode and decode.

public class LCLAdvantagePlusJackpotCache: Codable {
    public let token: String
    public let amount: NSNumber
    public let member: Bool

    public init(token: String, amount: NSNumber, member: Bool) {
        self.token = token
        self.amount = amount
        self.member = member
    }

    enum CodingKeys: String, CodingKey {
        case token, amount, member
    }

}

回答1:


It's because NSNumber is not Codable. Do not use Objective-C types; use Swift types. (That's a general rule; it isn't confined to the Codable situation. But this is a good example of why the rule is a good one!)

Change NSNumber to Int or Double (in both places where it occurs in your code) and all will be well.



来源:https://stackoverflow.com/questions/50609501/codable-does-not-conform-to-protocol-decodable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!