枚举值测试失败,如果有相关值? [英] Testing for enum value fails if one has associated value?

查看:133
本文介绍了枚举值测试失败,如果有相关值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在游乐场测试,我不知道该怎么做。有一个没有关联值的正常枚举,一切都很好。

 枚举CompassPoint {
case North
case南
case东
case西
}

var direction = CompassPoint.East

如果direction!= .West {
println(Go West!)
}

但是,如果我的一个枚举有一个关联的值,方向测试失败,出现这个错误:找不到成员'West'

 枚举CompassPoint {
case North(Int)
case South
case East
case West
}

var direction = CompassPoint.East

如果direction!= .West {
println(Go West!)
}

我可以做什么来允许这个测试?

解决方案

枚举自动 Equable ,当它们的原始值为 Equable 时。在第一种情况下,假设原始值为 Int ,但是如果您给出另一种特定类型,例如 UInt32 甚至 String



然而,添加关联值后,这种自动符合 Equable 不再发生,因为您可以声明:

  let littleNorth = CompassPoint.North(2)
let bigNorth = CompassPoint.North(99999)



<这些是平等的吗? Swift应该怎么知道?您必须通过将枚举声明为 Equable 然后实现 == 运算符:

 枚举CompassPoint:Equable {
case North(Int)
case South
case East
case West
}

public func ==(lhs:CompassPoint,rhs:CompassPoint) - > Bool {
switch(lhs,rhs){
case(.North(let lhsNum),.North(let rhsNum)):
return lhsNum == rhsNum
case(。 South,.Suth):return true
case(.East,.East):return true
case(.West,.West):return true
default:return false
}
}

现在,您可以测试平等或不平等,如下所示:

  let otherNorth = CompassPoint.North(2)
println(littleNorth == bigNorth)// false
println littleNorth == otherNorth)// true


I'm testing this in the Playground and I'm not sure how to do this. With a normal enum that doesn't have associated values, everything is fine.

enum CompassPoint {
    case North
    case South
    case East
    case West
}

var direction = CompassPoint.East

if direction != .West {
    println("Go West!")
}

However, if one of my enums has an associated value, the direction test fails with this error: could not find member 'West'

enum CompassPoint {
    case North(Int)
    case South
    case East
    case West
}

var direction = CompassPoint.East

if direction != .West {
    println("Go West!")
}

What can I do to allow for this test?

解决方案

Enumerations are automatically Equatable when they have a raw value that's Equatable. In your first case, the raw value is assumed to be Int, but it would work if you'd given it another specific type like UInt32 or even String.

Once you add an associated value, however, this automatic conformance with Equatable doesn't happen any more, since you can declare this:

let littleNorth = CompassPoint.North(2)
let bigNorth = CompassPoint.North(99999)

Are those equal? How should Swift know? You have to tell it, by declaring the enum as Equatable and then implementing the == operator:

enum CompassPoint : Equatable {
    case North(Int)
    case South
    case East
    case West
}

public func ==(lhs:CompassPoint, rhs:CompassPoint) -> Bool {
    switch (lhs, rhs) {
    case (.North(let lhsNum), .North(let rhsNum)):
        return lhsNum == rhsNum
    case (.South, .South): return true
    case (.East, .East): return true
    case (.West, .West): return true
    default: return false
    }
}

Now you can test for equality or inequality, like this:

let otherNorth = CompassPoint.North(2)
println(littleNorth == bigNorth)            // false
println(littleNorth == otherNorth)          // true

这篇关于枚举值测试失败,如果有相关值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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