Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift:比较扑克牌-无法应用二进制比较_Swift_Comparison Operators - Fatal编程技术网

Swift:比较扑克牌-无法应用二进制比较

Swift:比较扑克牌-无法应用二进制比较,swift,comparison-operators,Swift,Comparison Operators,我在Swift游乐场中有以下代码,其中创建了一个卡结构 我需要比较卡片,看看哪个值更高 理想情况下,我还需要检查所有形式的二进制操作 但是,我不断收到一个错误,上面写着: error: binary operator '>' cannot be applied to two 'Card' operands if (ace > king) { ~~~ ^ ~~~~ 另一条信息指出: 注意:“>”的重载与这些部分匹配的参数一起存在 列表:((),()),(UInt8,UInt8

我在Swift游乐场中有以下代码,其中创建了一个
结构

我需要比较卡片,看看哪个值更高

理想情况下,我还需要检查所有形式的二进制操作

但是,我不断收到一个错误,上面写着:

error: binary operator '>' cannot be applied to two 'Card' operands if (ace > king) {
    ~~~ ^ ~~~~
另一条信息指出:

注意:“>”的重载与这些部分匹配的参数一起存在 列表:((),()),(UInt8,UInt8),(Int8,Int8),(UInt16,UInt16), (Int16,Int16),(UInt32,UInt32),(UInt64,UInt64), (Int64,Int64),(UInt,UInt),(Int,Int),(UIContentSizeCategory, UIContentSizeCategory),(日期,日期),(IndexPath,IndexPath), 指数集指数,指数集指数,((A,B),(A,B)),((A,B,C),(A,B, C) ),((A,B,C,D),(A,B,C,D)),((A,B,C,D,E),(A,B,C,D,E)), ((A,B,C,D,E,F),(A,B,C,D,E,F)),(Self,Other),(Self,R)如果 (王牌>国王){


我想知道我错了什么。

您需要遵守
可比
协议,才能使用比较运算符(

扩展卡:可比{
静态功能<(左:卡,右:卡)->Bool{
返回lhs.rank.values.first
您需要符合
Compariable
才能使用比较运算符。您可以为卡对象创建自定义
=
,但不是
也不是
,这是
Compariable
协议:为什么jack、.queen、.king具有相同的值?当第二个值为alwa时,使用第二个值的目的是什么ys nil?@LeoDabus刚刚输入了我尝试过的相同问题:但是你的版本更好
public static func==(lhs:Card,rhs:Card)->Bool{return((lhs.rank==rhs.rank)&&(lhs.suit==rhs.suit))}public static func>(lhs:Card,rhs:Card)->Bool{return((lhs.rank.rawValue>rhs.rank.rawValue))}public static func<(lhs:Card,rhs:Card)->Bool{return((lhs.rank.rawValue
@zardon我同意比较rank属性,但是您不必要地实现了操作符
,您只需要实现具有类似一致性的
类型就可以实现小于操作符(啊,我明白。有道理。对不起,第一次用comparable做事情,但我相信我明白发生了什么
struct Card : Equatable {

    // nested Suit enumeration
    enum Suit: Character {
        case spades = "♠", hearts = "♡", diamonds = "♢", clubs = "♣"
    }

    // nested Rank enumeration
    enum Rank: Int {
        case two = 2, three, four, five, six, seven, eight, nine, ten
        case jack, queen, king, ace
        struct Values {
            let first: Int, second: Int?
        }
        var values: Values {
            switch self {
            case .ace:
                return Values(first: 11, second: nil)
            case .jack, .queen, .king:
                return Values(first: 10, second: nil)
            default:
                return Values(first: self.rawValue, second: nil)
            }
        }
    }

    // Card properties and methods
    let rank: Rank, suit: Suit
    var description: String {
        var output = "suit is \(suit.rawValue),"
        output += " value is \(rank.values.first)"
        if let second = rank.values.second {
            output += " or \(second)"
        }
        return output
    }
}


extension Card {
    public static func == (lhs: Card, rhs: Card) -> Bool {
        return ((lhs.rank == rhs.rank) && (lhs.suit == rhs.suit))
    }
}

// Try to compare two cards
let ace = Card(rank: .ace, suit: .clubs)
let king = Card(rank: .king, suit: .diamonds)

if (ace > king) {
    print ("Ace is higher value")
}
else {
    print ("Ace is NOT higher")
}
extension Card: Comparable {
    static func < (lhs: Card, rhs: Card) -> Bool {
        return lhs.rank.values.first < rhs.rank.values.first
    }
}