比较 Swift 中的弱泛型

Compare / Equatable weak generics in Swift

我想像这样创建一个弱引用委托数组...

fileprivate class WeakDelegate<T:AnyObject> {

    weak var value:T?

    init (value:T) {
        self.value = value
    }
}


class Radio {

    private var delegates = [WeakDelegate<AnyObject>]()
}

到目前为止一切顺利...?我还想做的是以这两种方式整理我的数组...

1.

func removeDeadDelegates() {

    let liveDelegates = delegates.filter { [=12=].value != nil }
    delegates = liveDelegates
}

和 2.

func remove<T>(specificDelegate:T) {

    let filteredDelegates = delegates.filter { [=13=].value != specificDelegate }
    listeners = filteredDelegates
}

Cannot convert value of type 'T' to expected argument type '_OptionalNilComparisonType'

现在我可以添加这个来让警告像这样消失...

    let liveDelegates = delegates.filter {

        if let d = specificDelegate as? _OptionalNilComparisonType {
            return [=14=].value != d
        }

        return true
    }

但是这个转换不起作用...

我很担心,因为我不确定这意味着什么...任何人都可以解释为什么我不能将泛型与 == 进行比较以及为什么此转换失败?

感谢您的宝贵时间

编辑

像这样?

func remove<T:AnyObject>(delegate:T) {

    let filteredDelegates = delegates.filter { [=15=].value != delegate }
    delegates = filteredDelegates
}

无喜可悲...

class 类型的实例可以与“等同于”进行比较 === 和“不等同于”!== 运算符:

func remove(specificDelegate: AnyObject) {
    let filteredDelegates = delegates.filter { [=10=].value !== specificDelegate }
    delegates = filteredDelegates
}

同样适用于泛型方法

func remove<T:AnyObject>(specificDelegate: T) {
    let filteredDelegates = delegates.filter { [=11=].value !== specificDelegate }
    delegates = filteredDelegates
}

(但我还没有看到这样做的好处)。