swift - 在 Swift 的协议(protocol)扩展中访问嵌套在结构中的枚举变量

标签 swift swift2

我正在实现一个转换器,它可以根据提供的单位样式(公制或英制)轻松地将运行的 double 类型参数转换为适当的单位。距离以米为单位输入,根据给定的单位样式转换为公里或英里。

enum 单元样式的两种状态

//enum to track the states of unit style
enum UnitStyle{
    case Metric, Imperial
}

protocol 由运行的每个参数实现。这需要模型实现一个名为 Unit 的类型,因为每个参数都有自己的单元类型。例如对于距离,它可能超出 km 或 mi,而对于速度,它可能超出 km/hr 或 mi/hr

//every parameter of run has to implement this protocol
protocol RunParameter{

    typealias Unit: RawRepresentable
    var value: Double{get set}
    var unit: Unit{get}
    var unitStyle: UnitStyle{get set}
}

此扩展程序具有所有魔力。它将给定运行参数的值乘以其合适的单位以获得正确的值。到这里为止一切都很好。现在我还需要在此方法中获取单位描述,以便我可以将其返回显示在屏幕上。为此,我在 enum Unit 中创建了一个 description 变量,但问题是我无法在协议(protocol)扩展中访问此 description 变量。 简而言之,self.unit 上只有 rawValue 变量可用,协议(protocol)扩展中没有 description 变量。

extension RunParameter where Unit.RawValue == Double{

mutating func getValueForUnitStyle(unitStyle: UnitStyle) -> Double{

    self.unitStyle = unitStyle
    return value * self.unit.rawValue
    //here I want to return both calculated value and unit string
    //but unable to access unit description on self.unit
   }
}

struct Distance: RunParameter {
   enum Unit: Double {

        case km = 0.001
        case m = 1.0
        case mi = 0.000621371

        var description: String{

        switch self{
        case .km: return "km"
        case .m: return "m"
        case .mi: return "mi"
        }
     }
 }

   var value: Double
   var unitStyle = UnitStyle.Metric
   var unit: Unit {

        get{

            switch unitStyle{
            case .Metric: return Unit.km
            case .Imperial: return Unit.mi
        }
    }
}

init(value: Double){

    self.value = value
   }
}

struct Run {
    var unitStyle = UnitStyle.Imperial
    var distance = Distance(value: 10.0)
}

description 变量可在此处获得。我可以访问关于 x.distance.unit

的描述
var x = Run()
let z = x.distance.getValueForUnitStyle(.Imperial)
x.distance.unit.description //output "mi"

最佳答案

enum UnitStyle{
    case Metric, Imperial
}

protocol RunParameter{

    typealias Unit: RawRepresentable, CustomStringConvertible
    var value: Double{get set}
    var unit: Unit{get}
    var unitStyle: UnitStyle{get set}
}
extension RunParameter where Unit.RawValue == Double{

    mutating func getValueForUnitStyle(unitStyle: UnitStyle) -> (Double,String){

        self.unitStyle = unitStyle
        return (value * self.unit.rawValue, self.unit.description)
        //here I want to return both calculated value and unit string
        //but unable to access unit description on self.unit
    }
}

struct Distance: RunParameter {
    enum Unit: Double, CustomStringConvertible {

        case km = 0.001
        case m = 1.0
        case mi = 0.000621371

        var description: String{

            switch self{
            case .km: return "km"
            case .m: return "m"
            case .mi: return "mi"
            }
        }
    }

    var value: Double
    var unitStyle = UnitStyle.Metric
    var unit: Unit {

        get{

            switch unitStyle{
            case .Metric: return Unit.km
            case .Imperial: return Unit.mi
            }
        }
    }

    init(value: Double){

        self.value = value
    }
}

struct Run {
    var unitStyle = UnitStyle.Imperial
    var distance = Distance(value: 10.0)
}

var x = Run()
let z = x.distance.getValueForUnitStyle(.Imperial)
x.distance.unit.description //output "mi"

print(z.0,z.1) // 0.00621371 mi

关于swift - 在 Swift 的协议(protocol)扩展中访问嵌套在结构中的枚举变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35286811/

相关文章:

ios - 单元测试 Swift 2.0、@testable 导入和方案目标问题

ios - 从不同的类调用 UITextField 的内容

swift - 将标签文本设置为 nil 或将其设置为 ""有什么区别?

ios - 了解具有泛型约束的泛型

ios - 快速避免索引超出范围错误

ios - 使用NotificationCenter在AVPlayer中替换视频文件

xcode - Xcode Swift 7.3播放音频文件

ios - 如何解码BLE特征值?

Obj-C 的 Swift 桥接头

ios - 无法使用参数列表类型(字符串 : String, 属性 : [NSObject: AnyObject]?)调用 NSAttributedString 类型的初始值设定项