Вот часть модели, в которой у меня возникла проблема:
Код: Выделить всё
struct MyModel: Codable {
let id: ID?
}
enum ID: Codable {
case integer(Int)
case string(String)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode(Int.self) {
self = .integer(x)
return
}
if let x = try? container.decode(String.self) {
self = .string(x)
return
}
throw DecodingError.typeMismatch(ID.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for ID"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .integer(let x):
try container.encode(x)
case .string(let x):
try container.encode(x)
}
}
}
Код: Выделить всё
Optional(MyApp.ID.integer(27681250))
Код: Выделить всё
Optional(MyApp.ID.string(27681250))
Код: Выделить всё
print(modelData?.id)
Я пробовал преобразовать его в другие типы, но это не помогает.
Любая помощь приветствуется.
Подробнее здесь: https://stackoverflow.com/questions/703 ... -with-cust
Мобильная версия