【发布时间】:2019-12-31 17:13:51
【问题描述】:
如何使用 AssociatedType 对协议执行一致性检查。 Xcode 显示错误:
Protocol 'MyListener' 只能用作通用约束,因为 它有 Self 或关联的类型要求
我的最终目标是从weakObjects 数组中提取“MyListener.section”,其中处理程序与函数参数匹配。
注意。 weakObjects 的 NSPointerArray 是假设捕获不同类型的 MyListeners。
public class MyHandler<O,E> {
var source = [O]()
var dest = [E]()
}
public protocol MyListener:class {
var section: Int {get}
associatedtype O
associatedtype E
var handler: MyHandler<O,E>? { get }
}
public class MyAnnouncer {
private let mapWeakObjects: NSPointerArray = NSPointerArray.weakObjects()
public func add<L: MyListener>(listener: L) {
let pointer = Unmanaged.passUnretained(listener).toOpaque()
mapWeakObjects.addPointer(pointer)
}
public func search<O, E> (h:MyHandler<O,E>) -> [Int] {
_ = mapWeakObjects.allObjects.filter { listener in
if listener is MyListener { // Compilation failed
}
if let _ = listener as? MyListener { //Compilation error
}
if listener is MyListener.Type { //Compilation failed
}
}
return [] // ultimate goal is to extract corresponding [MyListener.section].
}
}
【问题讨论】:
标签: swift swift-protocols conditional-operator associated-types