本文介绍了可编码:不符合协议“可解码"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无法弄清楚为什么我的课程不符合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.
更多推荐
可编码:不符合协议“可解码"
发布评论