如何在Swift泛型数据结构(协议类型)中使用弱引用?

如何在Swift泛型数据结构(协议类型)中使用弱引用?

问题描述:

我想在通用数据结构中使用弱引用;在里面 数组下面的示例,但通常是任何泛型类型.我几乎可以得到它 工作:(

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

我可以添加一个协议:

// 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

有什么我想念的吗?

错误消息使用'P'作为符合协议的具体类型 不支持"AnyObject"",表示这是对的临时限制 编译器.这会解决吗?我应该提交错误报告吗?

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,

-霍华德.

我在玩耍时发现:

// 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

有效:)

令人讨厌的是,您必须使用@objc,因此不能使用纯粹的Swift解决方案,但是由于我在iOS上使用,这对我来说不是问题.

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.