Search code examples
swiftprotocolsweak-references

Swift warning: 'weak' should not be applied to a property declaration in a protocol


Looks like weak references will be disallowed in protocols. So what am I supposed to do if I wanna add a weak reference? Any better idea?

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

Solution

  • Simply remove the weak keyword from the protocol and declare the property as weak in the conforming type instead:

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