繁体   English   中英

强制泛型类型参数是类类型?

[英]Force a generic type parameter to be a class type?

我试图找出一种方法,当循环中的某些引用保存在集合中时,避免保留周期。 我的想法是创建一个包装struct

struct Weak<T> {
    unowned let value: T

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

这里的问题是unowned main.swift:3:17: 'unowned' cannot be applied to non-class type 'T'; consider adding a class bound weak成员必须属于类类型( main.swift:3:17: 'unowned' cannot be applied to non-class type 'T'; consider adding a class bound ),但是没有合理的超类对我来说要求T继承。

有没有办法强制T成为类类型而不继承特定的其他类?

尝试:

struct Weak<T:AnyObject>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM