3

我有一个称为订阅者的数组,它存储符合 JABPanelChangeSubscriber 协议的对象。该协议被声明为

public protocol JABPanelChangeSubscriber {

}

我的数组被声明为:

var subscribers = [JABPanelChangeSubscriber]()

现在我需要实现一个方法来将订阅者添加到列表中,但它首先必须检查该订阅者之前是否尚未添加。

public func addSubscriber(subscriber: JABPanelChangeSubscriber) {
    if subscribers.find(subscriber) == nil { // This ensures that the subscriber has never been added before
        subscribers.append(subscriber)
    }
}

不幸的是,JABPanelChangeSubscriber 不是 Equatable,我不知道如何使它 Equatable,所以 find 方法给了我一个错误。任何人都可以帮助我解决问题或提出不同方法的建议吗?

谢谢

4

2 回答 2

4

假设实现您的协议的所有类型都是引用类型 (类),您可以将协议声明为“类协议”

public protocol JABPanelChangeSubscriber : class {

}

并使用标识运算符===检查数组是否已包含指向与给定参数相同的实例的元素:

public func addSubscriber(subscriber: JABPanelChangeSubscriber) {
    if !contains(subscribers, { $0 === subscriber } ) {
        subscribers.append(subscriber)
    }
}
于 2015-05-20T08:36:44.270 回答
0

向协议添加isEqualTo(:)要求JABPanelChangeSubscriber

public protocol JABPanelChangeSubscriber {
    func isEqualTo(other: JABPanelChangeSubscriber) -> Bool
}

有需求的JABPanelChangeSubscriber协议扩展Self

extension JABPanelChangeSubscriber where Self: Equatable {
   func isEqualTo(other: JABPanelChangeSubscriber) -> Bool {
      guard let other = other as? Self else { return false }
        return self == other
   }
}

记得让对象Equatable也符合协议

class SomeClass: Equatable {}
func == (lhs: SomeClass, rhs: SomeClass) -> Bool {
//your code here
//you could use `===` operand
}

struct SomeStruct: Equatable {}
func == (lhs: SomeStruct, rhs: SomeStruct) -> Bool {
//your code here
}

然后找到索引

func indexOf(object: JABPanelChangeSubscriber) -> Int? {
    return subcribers.indexOf({ $0.isEqualTo(object) })
}

如果要在将对象添加到数组之前检查对象是否存在

func addSubscriber(subscriber: JABPanelChangeSubscriber) {
    if !subscribers.contains({ $0.isEqualTo(subscriber) }) {
        subscribers.append(subscriber)
    }
}
于 2016-07-07T03:11:13.687 回答