在ns测量中自定义绵羊到山羊的转换?
如何将swift的测量库扩展到自定义转换单位?
示例是我想将绵羊转换为山羊。
答案 0 :(得分:4)
请参阅https://developer.apple.com/reference/foundation/nsdimension
示例实施:
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