Reputation: 11059
Custom sheeps to goats conversions in ns measurement?
How do I extend swift's measurement library to account for a custom unit of conversion?
Example is I want to convert sheeps to goats.
Upvotes: 2
Views: 663
Reputation: 57154
See https://developer.apple.com/reference/foundation/nsdimension
Example implementation:
class Animal : Dimension {
static let sheep = Animal(symbol: "Sh", converter: UnitConverterLinear(coefficient: 2.0))
static let goat = Animal(symbol: "Go", converter: UnitConverterLinear(coefficient: 1.0))
static let elephant = Animal(symbol: "El", converter: UnitConverterLinear(coefficient: 100.0))
override class func baseUnit() -> Animal {
return Animal.goat
}
}
var x = Measurement(value:5, unit: Animal.sheep) // 5.0 Sh
x.convert(to: Animal.goat) // 10.0 Go
x.convert(to: Animal.elephant) // 0.1 El
Upvotes: 4