可编码:不符合协议“可解码" [英] Codable : does not conform to protocol 'Decodable'

查看:63
本文介绍了可编码:不符合协议“可解码"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法弄清楚为什么我的课程不符合Codable请注意,就我而言,我不需要实现 encoding decode 的方法.

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
    }

}

推荐答案

这是因为NSNumber不可编码.不要使用Objective-C类型;使用Swift类型.(这是一条一般规则;它不限于可编码的情况.但这是为什么该规则是一个好规则的一个很好的例子!)

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!)

将NSNumber更改为Int或Double(在代码中出现的两个地方),一切都会好起来.

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

这篇关于可编码:不符合协议“可解码"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆