如何通过忽略它在Swift中的相关值来比较枚举和相关联的值? [英] How to compare enum with associated values by ignoring its associated value in Swift?

查看:111
本文介绍了如何通过忽略它在Swift中的相关值来比较枚举和相关联的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读如何测试Swift枚举的平等性与相关值,我实现了以下枚举:

 枚举CardRank {
case Number
case Jack
case Queen
case King
case Ace
}

func ==(a:CardRank,b:CardRank) > bool {
switch(a,b){
case(.Number(let a),.Number(let b))其中a == b:return true
case .jack):return true
case(.Queen,.Queen):return true
case(.King,.King):return true
case(.Ace,.Ace):return true
default:return false
}
}

代码工程:

 让卡:CardRank = CardRank.Jack 
如果卡== CardRank.Jack {
print(You played a jack!)
} else if card == CardRank.Number(2){
print(A two can not be played in this time。 }

但是,这不会编译:

  let number = CardRank.Number(5)
if number == CardRank.Number {
print(You must play a face card! b $ b}

...并显示以下错误讯息:


二进制运算符'=='不能应用于类型'CardRank'和'(Int) - > CardRank'的操作数


我假设这是因为它期望一个完整的类型和 CardRank.Number 不指定整个类型,而 CardRank.Number(2)没有。但是,在这种情况下,我希望它匹配任何号码;



显然我可以使用switch语句,但是实现 == 操作符是为了避免这种详细的解决方案:

 开关号{
case .Number:
print 你必须玩一张面孔卡!)
默认值:
break
}

有没有办法比较一个枚举和相关的值,而忽略它的相关值?



可以将 == 方法中的情况更改为 case(.Number,.Number):return true 虽然它会正确返回true,我的比较仍然会看起来像它的特定数字( number == CardRank.Number(2);其中2是一个虚值)而不是任何号码( number == CardRank.Number )。

解决方案

编辑:由于Etan指出,您可以省略(_)通配符匹配, 。






不幸的是,我不相信有一个比你的 code>在Swift 1.2中。



但是,在Swift 2中,可以使用新的 if-case 模式匹配:

  let number = CardRank.Number(5)
如果case .Number(_)=数字{
//是数字
} else {
//其他
}

如果你想避免冗长,你可以考虑在你的enum中添加一个 isNumber 计算属性来实现你的switch语句。 / p>

After reading How to test equality of Swift enums with associated values, I implemented the following enum:

enum CardRank {
    case Number(Int)
    case Jack
    case Queen
    case King
    case Ace
}

func ==(a: CardRank, b: CardRank) -> Bool {
    switch (a, b) {
    case (.Number(let a), .Number(let b))   where a == b: return true
    case (.Jack, .Jack): return true
    case (.Queen, .Queen): return true
    case (.King, .King): return true
    case (.Ace, .Ace): return true
    default: return false
    }
}

The following code works:

let card: CardRank = CardRank.Jack
if card == CardRank.Jack {
    print("You played a jack!")
} else if card == CardRank.Number(2) {
    print("A two cannot be played at this time.")
}

However, this doesn't compile:

let number = CardRank.Number(5)
if number == CardRank.Number {
    print("You must play a face card!")
}

... and it gives the following error message:

Binary operator '==' cannot be applied to operands of type 'CardRank' and '(Int) -> CardRank'

I'm assuming this is because it's expecting a full type and CardRank.Number does not specify an entire type, whereas CardRank.Number(2) did. However, in this case, I want it to match any number; not just a specific one.

Obviously I can use a switch statement, but the whole point of implementing the == operator was to avoid this verbose solution:

switch number {
case .Number:
    print("You must play a face card!")
default:
    break
}

Is there any way to compare an enum with associated values while ignoring its associated value?

Note: I realize that I could change the case in the == method to case (.Number, .Number): return true, but, although it would return true correctly, my comparison would still look like its being compared to a specific number (number == CardRank.Number(2); where 2 is a dummy value) rather than any number (number == CardRank.Number).

解决方案

Edit: As Etan points out, you can omit the (_) wildcard match to use this more cleanly.


Unfortunately, I don't believe that there's an easier way than your switch approach in Swift 1.2.

In Swift 2, however, you can use the new if-case pattern match:

let number = CardRank.Number(5)
if case .Number(_) = number {
    // Is a number
} else {
    // Something else
}

If you're looking to avoid verbosity, you might consider adding an isNumber computed property to your enum that implements your switch statement.

这篇关于如何通过忽略它在Swift中的相关值来比较枚举和相关联的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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