Swift警告:“weak”不应应用于协议中的属性声明

h6my8fg2  于 11个月前  发布在  Swift
关注(0)|答案(2)|浏览(92)

看起来像weak references will be disallowed in protocols。如果我想添加一个弱引用,我该怎么做呢?有更好的主意吗?

protocol PipelineElementDelegate: class {
    func someFunc()
}
protocol PipelineElement {
    weak var delegate: PipelineElementDelegate? { get set}
}
z6psavjg

z6psavjg1#

只需从协议中删除weak关键字,并在符合类型中将属性声明为weak:

class SomeClass: PipelineElement {
    weak var delegate: PipelineElementDelegate?
}
vc6uscn9

vc6uscn92#

将'objc'添加到协议定义和具体类类型中,您可以在协议中使用'weak'。还要确保具体类符合NSObject,即

@objc protocol Calculation : AnyObject
{
  weak var viewModelDelegate: CalculationsViewModel? { get set }
}

@objc final class CalculationsViewModel: NSObject, ObservableObject
{
}

相关问题