【发布时间】:2023-03-22 20:23:02
【问题描述】:
我有一个可编码的枚举,它可以采用字符串或双精度的形式,因为我得到的 JSON 响应可以是字符串或双精度。我需要从枚举中提取双精度,但我不知道为什么。
enum greeksEnum: Codable, Equatable
{
func encode(to encoder: Encoder) throws {
}
case double(Double), string(String)
init(from decoder: Decoder) throws
{
if let double = try? decoder.singleValueContainer().decode(Double.self)
{
self = .double(double)
return
}
if let string = try? decoder.singleValueContainer().decode(String.self)
{
self = .string(string)
return
}
throw greekError.missingValue
}
enum greekError:Error
{
case missingValue
}
}
如何将双精度值提取到双精度变量中?
这是我比较字符串的方式:
if (volatility[0] == optionsApp.ExpDateMap.greeksEnum.string("NaN"))
{
}
但是当我尝试将枚举类型转换为 Double 类型时,我得到了这个错误。
self.IV = Double(volatility[0])
Initializer 'init(_:)' 要求 'ExpDateMap.greeksEnum' 符合 'BinaryInteger'
【问题讨论】:
标签: ios swift enums codable equatable