如何在Swift中表示质量的大小? [英] How to represent magnitude for mass in Swift?
问题描述
// SI单位
枚举幅度:度量{
case Milli = Measurement(-3,ml)
case Centi = Measurement -2,cl)
case Desi = Measurement(-1,dl)
case Gram = Measurement(0,g)
case Kilo = Measurement(3,公斤)
}
class测量{
let magnitude:Int
let shorthandName:String
init(magnitude:Int,shorthandName:String){
self.magnitude = magnitude
self.shorthandName = shorthandName
}
}
我正在实施转换器作为应用程序的一部分。我已经开始在不同质量的范围之间进行转换。我认为将Magnitude作为一个枚举很好,与其短手名称紧密相连。但是Swift不允许我有一个枚举对象,因为原始值需要一个字面值。任何人都知道一个更聪明的方式来表示大小,速记名和单位之间的转换之间的联系?
解决方案如果你想可以使用 String
指定质量单位,也许您可以使用 String
来表示速记名称,变量为您提供有关单位的更多信息,例如量级
。以下是一个例子:
1。 MassUnit
枚举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
}
}
扩展名MassUnit:可打印{
var description:String {
return self.rawValue
}
}
2。然后,为了存储实际质量,您可以使用 Struct
,这也可以处理转换。例如:
struct Mass {
var value:Double
var unit:MassUnit
static func convertMass(mass:Mass,toUnit unit:MassUnit) - >质量{
let ordersOfMagnitude = MassUnit.ordersOfMagnitudeFrom(mass.unit,to:unit)
let multiplier = pow(10.0,Double(ordersOfMagnitude))
return质量(value:mass.value *乘数,单位:单位)
}
//返回可选的质量,因为我们不能确定
// unitString将代表一个MassUnit。
static func convertMass(mass:Mass,toUnit unitString:String) - >质量是多少? {
如果let unit = MassUnit(rawValue:unitString){
return convertMass(mass,toUnit:unit)
}
return nil
}
}
扩展质量{
init?(value:Double,_ unitString:String){
如果let unit = MassUnit(rawValue:unitString){
self = Mass(value:value,unit:unit)
} else {
return nil
}
}
}
extension质量:可打印{
var description:String {
return\(value)\(unit)
}
}
3。然后,您可以使用质量和单位:
如果let mass = Mass(value:1,kg ),
let convertedMass = Mass.convertMass(mass,toUnit:g){
println(\(mass))转换为\(MassUnit.Grams)等于\ (convertedMass))
//打印:1.0 kg转换为g等于1000.0 g
}
但是,如果您使用不可转换为 MassUnit
的 unitString
(在创建或转换时,将返回 nil
。例如:
let mass = Mass(value:1,NotAUnit)// nil
// 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
}
}
I am implementing a converter as a part of an app. I've started with converting between the different magnitudes of mass. I was thinking its nice to have the Magnitude as an enum, strongly connected to its short hand name. But Swift does not let me have an enum with objects, because of the raw value needs to be a literal. Anyone know a more clever way of representing the connection between magnitude, its shorthand name and converting between the units?
解决方案 If you want to be able to specify the unit of the mass with a String
perhaps you could use String
to represent the shorthand name and variables to give you more information about the unit, such as magnitude
. Here's an example:
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. Then, for storing actual masses you could use a Struct
, which could also handle the conversions. For example:
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. Then you can use the masses and units:
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
}
However if you use a unitString
that isn't convertible to a MassUnit
(either when creating or converting) nil
will be returned. For example:
let mass = Mass(value: 1, "NotAUnit") // nil
这篇关于如何在Swift中表示质量的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!