二元运算符“==”不能应用于两个操作数

Binary operator '==' cannot be applied to two operands

我有一个 class 协议 Equatable。 class 看起来像这样:

class Item: Equatable {

    let item: [[Modifications: String]]

    init(item: [[Modifications: String]]) {
        self.item = item
    }
}

func ==(lhs: Item, rhs: Item) -> Bool {
    return lhs.item == rhs.item
}

但这给了我错误(见标题)。 属性 item 之前是 [[String: String]] 并且没有问题,我不知道如何解决这个问题。我试过谷歌搜索和搜索,但没有运气..

枚举只是一个简单的基本枚举:

enum Modifications: Int {
    case Add    = 1
    case Remove = 2
    case More   = 3
    case Less   = 4
}

Item 对象的 == 函数中,您需要进一步指定如何比较两种类型的字典数组(具体来说,两种类型的 [[Modifications: String]])。

以下工作解决方案逐个元素(逐个字典)比较您的 item 数组,并且 == returns 仅当数组包含相同数量的字典时才为真,并且如果所有条目都相似 并且在词典数组中以相同的方式排序

func ==(lhs: Item, rhs: Item) -> Bool {

    if lhs.item.count == rhs.item.count {
        for (i, lhsDict) in lhs.item.enumerate() {
            if lhsDict != rhs.item[i] {
                return false
            }
        }
        return true
    }
    else {
        return false
    }
}

class Item : Equatable {

    let item: [[Modifications: String]]

    init(item: [[Modifications: String]]) {
        self.item = item
    }
}

您可能想将其修改为您实际想要用于比较的形式,但我希望您能领会其中的要点。

另请注意,如果在操场上进行测试,重要的是您的 == 函数定义 func ==(lhs: Item, rhs: Item) -> Bool { .. 应该 优先于 您的 class定义,否则你会得到不符合 Equatable 的错误。

更新: SE-0143 Conditional conformances 已在 Swift 4.2.

中实现

因此,您的代码现在可以编译了。如果您将 Item 定义为 struct

struct Item: Equatable {
    let item: [[Modifications: String]]

    init(item: [[Modifications: String]]) {
        self.item = item
    }
}

然后编译器自动合成==运算符, 比较 SE-0185 Synthesizing Equatable and Hashable conformance


(前Swift 4.1 回答:)

问题是即使==定义为字典类型 [Modifications: String],那个类型不符合 Equatable。因此数组比较运算符

public func ==<Element : Equatable>(lhs: [Element], rhs: [Element]) -> Bool

无法应用于 [[Modifications: String]]

Item== 可能的简洁实现是

func ==(lhs: Item, rhs: Item) -> Bool {
    return lhs.item.count == rhs.item.count 
           && !zip(lhs.item, rhs.item).contains {[=12=] !=  }
}

你的代码为 [[String: String]] 编译——如果基金会 框架被导入,正如@user3441734 正确地说的那样——因为 [String: String] 会自动转换为 NSDictionary,它符合 Equatable。这是该声明的 "proof":

func foo<T : Equatable>(obj :[T]) {
    print(obj.dynamicType)
}

// This does not compile:
foo( [[Modifications: String]]() )

// This compiles, and the output is "Array<NSDictionary>":
foo( [[String: String]]() )