【发布时间】:2023-01-28 01:31:18
【问题描述】:
我有一个可编码的枚举:
public enum MyEnum: String, Codable, Hashable, Sendable {
case one = "ONE"
case two = "TWO"
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawValue = try container.decode(RawValue.self)
self = MyEnum(rawValue: rawValue) ?? .one
}
}
但是,我现在需要使其与 objective-c 兼容。我知道我不能有String原始值,它必须是Int。我仍然需要它像以前一样兼容,因为它是从 JSON 创建的,JSON 是一个字符串而不是一个 Int。我该怎么做呢?
【问题讨论】: