Swift 在我扩展 CollectionType 时不调用我的“==”运算符重载

Swift doesn't call my "==" operator overload when I extend CollectionType

我有一个继承自 NSManagedObject 的自定义 class (VotingOption),有时我想检查数组中的某些投票选项是否重复。我试图使我的代码尽可能通用。这就是我为扩展 CollectionType 协议所做的:

extension CollectionType where Self.Generator.Element : Equatable {

    var duplicates: [Self.Generator.Element]{
        return = self.filter { element in
            return self.filter { [=11=] == element }.count != 1
        }
    }

    var hasDuplicates: Bool {
        return (self.duplicates.count != 0)
    }
}

这就像一个魅力,除了它没有使用全局函数:

func ==(lhs: VotingOption, rhs: VotingOption) -> Bool {
     return (lhs.location?.title.lowercaseString == rhs.location?.title.lowercaseString) && (lhs.startDate == rhs.startDate)
}

当我做这样的事情时:

let temp: [VotingOption] = votingOptions?.array as? [VotingOption]
if temp.hasDuplicates {
     //do something
}

当我像这样在 VotingOption 中扩展 isEqual 时:

class VotingOption: NSManagedObject {

    override func isEqual(object: AnyObject?) -> Bool {

        if let rhs = object as? VotingOption {
            return (self.location?.title.lowercaseString == rhs.location?.title.lowercaseString) && (self.startDate == rhs.startDate)
        } else {
            return false
        }
    }
    ...
    ...
    ... rest of class
}

应用程序崩溃并指向 AppDelegate 并出现 "libc++abi.dylib: terminating with uncaught exception of type NSException" 错误

如何让CollectionType中的“==”使用VotingOption的全局函数?

这是一个实现 duplicateshasDuplicates 两次的解决方案,一次用于 Equatable 元素,一次用于 VotingOptions class。为了尽可能减少代码重复,我定义了一个用于查找重复项的通用实现,它允许您传递比较两个元素的 function/closure:

extension CollectionType {

    func findDuplicates(checkEqual: (Self.Generator.Element, Self.Generator.Element) -> Bool) -> [Self.Generator.Element]{
        return self.filter { element in
            return self.filter { checkEqual([=10=], element) }.count != 1
        }
    }
}

extension CollectionType where Self.Generator.Element : Equatable {

    var duplicates: [Self.Generator.Element]{
        return self.findDuplicates(==)
    }

    var hasDuplicates: Bool {
        return (self.duplicates.count != 0)
    }
}

extension CollectionType where Self.Generator.Element : VotingOption {

    var duplicates: [Self.Generator.Element]{
        return self.findDuplicates {lhs, rhs in
            return (lhs.location?.title.lowercaseString == rhs.location?.title.lowercaseString) && (lhs.startDate == rhs.startDate)
        }
    }

    var hasDuplicates: Bool {
        return (self.duplicates.count != 0)
    }
}

感谢所有帮助过的人。我将post最终实现在这里: 基本上用方法 "equals" 创建了一个新协议 "Identity" ,它基本上是“==”,最后为实现 "Identity" 的 NSManagedObjects 添加了一个通用扩展,它基本上在 "equals"方法。但是在 "VotingOption" 中,我已经根据我的特定需要覆盖了这个函数。最后,扩展了集合类型,其中 "Element" 为 "Identity"。它调用 "equals" 方法而不是“==”:)

protocol Identity {
     func equals(rhs: Self) -> Bool
}

extension Identity where Self: NSManagedObject {
     func equals(rhs: Self) -> Bool {
          return self == rhs
     }
}

extension CollectionType where Self.Generator.Element : Identity {

    var duplicates: [Self.Generator.Element] {

        return self.filter { element in
            return self.filter { [=10=].equals(element) }.count != 1
        }
    }

    var hasDuplicates: Bool {
        return (self.duplicates.count > 0)
    }
}

class VotingOption: NSManagedObject, Identity {

    func equals(rhs: VotingOption) -> Bool {

    //... implementation here
    }
}