How can you use weak references in Swift generic data structure typed as a protocol?

隐身守侯 提交于 2019-12-10 17:42:35

问题


I am wanting to use weak references in generic data structures; in the example below an Array, but in general any generic type. I can almost get it to work :(

My experiments started off well; the following works:

// Array of weak references OK
struct WeakReference<T: AnyObject> {
    weak var value: T?
}
class C {
    var i: Int = 0
}
let c = C() // Strong reference to prevent collection
let weakCs = [WeakReference(value: c)] // OK
print("C: \(weakCs[0].value!.i)") // 0

I can add a protocol:

// Array of weak references that implements a protocol OK
protocol P: AnyObject { // Note AnyObject
    var i: Int { get }
}
class CP: P {
    var i: Int = 0
}
let cP = CP() // Strong reference to prevent collection
let weakCPs = [WeakReference(value: cP)] // OK
print("CP: \(weakCPs[0].value!.i)") // 0

But when I want an array of weak references to the protocol I get an error:

// Array of weak references of a protocol not OK
let weakPs: [WeakReference<P>] = [WeakReference(value: cP)] // Using 'P' as
a concrete type conforming to protocol 'AnyObject' is not supported
print("P: \(weakPs[0].value!.i)") // 0

Is there something I have missed?

The error message, "Using 'P' as a concrete type conforming to protocol 'AnyObject' is not supported", implies that it is a temporary limitation of the compiler. Is this going to be fixed? Should I lodge a bug report?

Thanks in advance for any advice,

-- Howard.


回答1:


Playing around I have found that:

// Array of weak references of a protocol OK so long as protocol marked @objc
@objc protocol P { // Note @objc
    var i: Int { get }
}
class CP: P {
    var i: Int = 0
}
let weakPs: [WeakReference<P>] = [WeakReference(value: cP)] // Note typed as `[WeakReference<P>]`
print("P: \(weakPs[0].value!.i)") // 0

works :)

It is annoying that you have to use @objc and therefore not a pure Swift solution, but since I am on iOS not a problem for me.



来源:https://stackoverflow.com/questions/39241734/how-can-you-use-weak-references-in-swift-generic-data-structure-typed-as-a-proto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!