可编码:不符合协议“可解码"

编程入门 行业动态 更新时间:2024-10-14 22:21:29
本文介绍了可编码:不符合协议“可解码"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

无法弄清楚为什么我的课程不符合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.

更多推荐

可编码:不符合协议“可解码"

本文发布于:2023-11-26 08:45:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不符合   协议   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!