Swift 3 ns 测量的自定义扩展?前任.绵羊到山羊 [英] Swift 3 Custom extension of ns measurement? Ex. Sheeps to goats

查看:15
本文介绍了Swift 3 ns 测量的自定义扩展?前任.绵羊到山羊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ns 测量中自定义绵羊到山羊的转换?

Custom sheeps to goats conversions in ns measurement?

如何扩展 swift 的度量库以考虑自定义转换单位?

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.

推荐答案

参见 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

这篇关于Swift 3 ns 测量的自定义扩展?前任.绵羊到山羊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆