如何在带参数的枚举上进行if-else比较

Zon*_*ame 29 enums swift

语言:Swift2.3

例如,让我向您展示不同类型的枚举

enum Normal {
    case one
    case two, three
}

enum NormalRaw: Int {
    case one
    case two, three
}

enum NormalArg {
    case one(Int)
    case two, three
}   
Run Code Online (Sandbox Code Playgroud)

Switch 可以在所有三个枚举上使用,如下所示:

var normal: Normal = .one
var normalRaw: NormalRaw = .one
var normalArg: NormalArg = .one(1)

switch normal {
    case .one: print("1")
    default: break
}

switch normalRaw {
    case .one: print(normalRaw.rawValue)
    default: break
}

switch normalArg {
    case .one(let value): print(value)
    default: break
}
Run Code Online (Sandbox Code Playgroud)

在if-else语句中虽然我只能对Normal和进行比较NormalRaw,并显示错误消息NormalArg,所以我无法运行代码

二进制运算符'=='不能应用于类型NormalArg 和的操作数_

这是代码示例:

if normal == .two { // no issue
    .. do something
}

if normalRaw == .two { // no issue
    .. do something
}

if normalArg == .two { // error here (the above message)
    .. do something
}

if normalArg == .one(_) { // error here (the above message)
    .. do something
}

if normalArg == .three { // error here (the above message)
    .. do something
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我真的没有用这个代码做任何事情,我只是想知道为什么我们不能做比较.

xxt*_*axx 86

诀窍是不用==实际检查,而是case在if语句中使用关键字和单个=.这在开始时有点反直觉,但就像if let你很快就习惯了:

enum Normal {
    case one
    case two, three
}

enum NormalRaw: Int {
    case one = 1
    case two, three
}

enum NormalArg {
    case one(Int)
    case two, three
}


let normalOne = Normal.one
let normalRawOne = NormalRaw.one
let normalArgOne = NormalArg.one(1)

if case .one = normalOne {
    print("A normal one") //prints "A normal one"
}

if case .one = normalRawOne {
    print("A normal \(normalRawOne.rawValue)") //prints "A normal 1"
}

if case .one(let value) = normalArgOne {
    print("A normal \(value)") //prints "A normal 1"
}
Run Code Online (Sandbox Code Playgroud)

关键是,在Swift中,如果你的枚举使用原始类型或者你没有相关的值,你只能获得免费的枚举方程式(尝试一下,你不能同时拥有两者).然而,Swift不知道如何将案例与相关值进行比较 - 我的意思是怎么样?我们来看看这个例子:

Normal.one == .one //true
Normal.one == .two //false

NormalRaw.one == .one //true
NormalRaw.one == .two //false

NormalArg.one(1) == .one(1) //Well...?
NormalArg.one(2) == .one(1) //Well...?
NormalArg.one(1) == .two //Well...?
Run Code Online (Sandbox Code Playgroud)

也许这更清楚了为什么这不能开箱即用:

class Special {
    var name: String?
    var special: Special?
}

enum SpecialEnum {
    case one(Special)
    case two
}

var special1 = Special()
special1.name = "Hello"

var special2 = Special()
special2.name = "World"
special2.special = special1

SpecialEnum.one(special1) == SpecialEnum.one(special2) //Well...?
Run Code Online (Sandbox Code Playgroud)

因此,如果您希望枚举具有关联值,则必须自己在枚举中实现Equatable协议:

enum NormalArg: Equatable {
    case one(Int)
    case two

    static func ==(lhs: NormalArg, rhs: NormalArg) -> Bool {
        switch (lhs, rhs) {
        case (let .one(a1), let .one(a2)):
            return a1 == a2
        case (.two,.two):
            return true
        default:
            return false
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我不知道谁想到了这种语法(如果 case .option = enum),但这很糟糕,IDE 甚至无法猜测您要检查哪个枚举来为您提供选项,并且如果您不知道选项对于这个枚举,您将不得不深入挖掘并浪费大量时间。 (3认同)
  • 我不知道我可以在 if 语句中添加 `case` 关键字。 (2认同)

aas*_*sya 7

答案是平等协议

现在让我们看看它是如何工作的。

例如考虑以下枚举:

enum Barcode {
    case upca(Int, Int)
    case qrCode(String)
    case none
}
Run Code Online (Sandbox Code Playgroud)

如果我们==在枚举上检查等于运算符,它将失败。

// Error: binary operator '==' cannot be applied to two Barcode operands
Barcode.qrCode("code") == Barcode.qrCode("code")
Run Code Online (Sandbox Code Playgroud)

如何使用平等协议解决此问题?

extension Barcode: Equatable {
}

func ==(lhs: Barcode, rhs: Barcode) -> Bool {
    switch (lhs, rhs) {
    case (let .upca(codeA1, codeB1), let .upca(codeA2, codeB2)):
        return codeA1 == codeA2 && codeB1 == codeB2

    case (let .qrCode(code1), let .qrCode(code2)):
        return code1 == code2

    case (.None, .None):
        return true

    default:
        return false
    }
}

Barcode.qrCode("code") == Barcode.qrCode("code") // true
Barcode.upca(1234, 1234) == Barcode.upca(4567, 7890) // false
Barcode.none == Barcode.none // true
Run Code Online (Sandbox Code Playgroud)