I have a custom data type:
struct Amount: ExpressibleByFloatLiteral, Codable {
typealias FloatLiteralType = Double
init(floatLiteral value: Double) {
self.value = value
}
init(value: Double) {
self.value = value
}
var value: Double
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(NSDecimalNumber(value: value).stringValue)
}
}
And when I create model with it:
struct Car {
var cost: Amount
}
var car = Car(cost: 1.5)
But when I set double typed value to model it not be inited:
var cost: Double = 1.5
var car = Car(cost: cost) // it won't be inited
Cannot convert value of type 'Double' to expected argument type 'Amount'
How to set double value to custom type?