当前位置 : 主页 > 手机开发 > 其它 >

如何在Swift中表示质量的大小?

来源:互联网 收集:自由互联 发布时间:2021-06-11
// SI unitsenum Magnitude : Measurement { case Milli = Measurement(-3, "ml") case Centi = Measurement(-2, "cl") case Desi = Measurement(-1, "dl") case Gram = Measurement(0, "g") case Kilo = Measurement(3,"kg")}class Measurement { let magnit
// SI units
enum Magnitude : Measurement {
    case Milli = Measurement(-3, "ml")
    case Centi = Measurement(-2, "cl")
    case Desi = Measurement(-1, "dl")
    case Gram = Measurement(0, "g")
    case Kilo = Measurement(3,"kg")
}


class Measurement {

    let magnitude : Int
    let shorthandName : String

    init(magnitude: Int, shorthandName: String) {
        self.magnitude = magnitude
        self.shorthandName = shorthandName
    }

}

我正在将转换器作为应用程序的一部分来实现.我开始在不同质量的大小之间进行转换.我认为将Magnitude作为枚举很好,与其简短的名字紧密相关.但是Swift并没有让我有一个包含对象的枚举,因为原始值需要是一个字面值.任何人都知道一种更聪明的方式来表示幅度,速记名称和单位之间的转换之间的联系吗?

如果您希望能够使用String指定质量单位,也许您可​​以使用String来表示简写名称和变量,以便为您提供有关单位的更多信息,例如幅度.这是一个例子:

1. MassUnit

enum MassUnit: String {
    case Milligrams = "mg"
    case Grams      = "g"
    case Kilos      = "kg"
    case Tons       = "t"

    var magnitude: Int {
        let mag: Int

        switch self {
            case .Milligrams: mag = -3
            case .Grams     : mag =  0
            case .Kilos     : mag =  3
            case .Tons      : mag =  6
        }

        return mag
    }


    static func ordersOfMagnitudeFrom(unit1: MassUnit, to unit2: MassUnit) -> Int {
        return unit1.magnitude - unit2.magnitude
    }
}

extension MassUnit: Printable {
    var description: String {
        return self.rawValue
    }
}

2.然后,为了存储实际质量,您可以使用Struct,它也可以处理转换.例如:

struct Mass {
    var value : Double
    var unit  : MassUnit

    static func convertMass(mass: Mass, toUnit unit: MassUnit) -> Mass {
        let ordersOfMagnitude = MassUnit.ordersOfMagnitudeFrom(mass.unit, to: unit)

        let multipler = pow(10.0, Double(ordersOfMagnitude))

        return Mass(value: mass.value * multipler, unit: unit)
    }

    //  Returns an optional Mass because we can't know for sure 
    //  unitString will represent a MassUnit.
    static func convertMass(mass: Mass, toUnit unitString: String) -> Mass? {
        if let unit = MassUnit(rawValue: unitString) {
            return convertMass(mass, toUnit: unit)
        }

        return nil
    }
}

extension Mass {
    init?(value: Double, _ unitString: String) {
        if let unit = MassUnit(rawValue: unitString) {
            self = Mass(value: value, unit: unit)
        } else {
            return nil
        }
    }
}

extension Mass : Printable {
    var description: String {
        return "\(value) \(unit)"
    }
}

3.然后你可以使用群众和单位:

if let mass = Mass(value: 1, "kg"),
   let convertedMass = Mass.convertMass(mass, toUnit: "g") {

    println("\(mass) converted to \(MassUnit.Grams) equals \(convertedMass)")

    // Prints: 1.0 kg converted to g equals 1000.0 g
}

但是,如果使用不可转换为MassUnit的unitString(在创建或转换时),将返回nil.例如:

let mass = Mass(value: 1, "NotAUnit") // nil
网友评论