gpt4 book ai didi

swift - 可编码 : does not conform to protocol 'Decodable'

转载 作者:搜寻专家 更新时间:2023-10-30 22:24:50 36 4
gpt4 key购买 nike

无法理解为什么我的类不符合 Codable请注意,在我的例子中,我不需要实现方法encodedecode

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 不是 Codable。不要使用 Objective-C 类型;使用 Swift 类型。 (这是一条一般规则;它并不局限于 Codable 情况。但这是一个很好的例子,说明了为什么该规则是一个好规则!)

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

关于swift - 可编码 : does not conform to protocol 'Decodable' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50609501/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com