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

编程入门 行业动态 更新时间:2024-10-25 16:23:44
本文介绍了如何在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.

更多推荐

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

本文发布于:2023-10-14 22:40:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据结构   协议   类型   如何在   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!