当我扩展 CollectionType 时,Swift 不会调用我的 "=="运算符重载

标签 swift equatable

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

extension CollectionType where Self.Generator.Element : Equatable {

    var duplicates: [Self.Generator.Element]{
        return = self.filter { element in
            return self.filter { $0 == 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:终止并出现 NSException 类型的未捕获异常”错误

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

最佳答案

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

extension CollectionType {

    func findDuplicates(checkEqual: (Self.Generator.Element, Self.Generator.Element) -> Bool) -> [Self.Generator.Element]{
        return self.filter { element in
            return self.filter { checkEqual($0, 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)
    }
}

关于当我扩展 CollectionType 时,Swift 不会调用我的 "=="运算符重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35300160/

相关文章:

Swift:在 Container View/NSView 内的 NSViewController 之间切换

ios - Swift 中的 AVPlayer 状态监听器

swift - 怎么理解这句话 "two instances with the same hash value don’ t 一定比较相等。 “

swift - 'Generic' 与 Swift 2.2 中的协议(protocol) 'Equatable' 的冗余一致性

flutter - 为什么我收到 Equatable 错误 : The element type can't be assigned to the list type 'Object'

ios - MAPKIT 当前用户位置更改的监听器 Swift 3

xcode - 在 XCode 7.2 中为 Swift 代码自动生成文档模板

swift - 在子类 Swift 中覆盖静态变量

Swift - 具有可等同变量的结构

ios - 测试定义为 Any 的值的数组包含,但保证属于同一类型。