15

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}
}
zoul
  • 102,279
  • 44
  • 260
  • 354
boog
  • 1,813
  • 3
  • 18
  • 21

2 Answers2

36

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?
}
zoul
  • 102,279
  • 44
  • 260
  • 354
0

Add 'objc' to the protocol definition and the concrete class type and you can use 'weak' inside the protocol. Also ensure the concrete class conforms to NSObject i.e.

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

@objc final class CalculationsViewModel: NSObject, ObservableObject
{
}
iOSProgrammingIsFun
  • 1,418
  • 1
  • 15
  • 32