2

I came across the following code and am not sure why it doesn’t compile:

protocol CellDelegate: class {}

protocol DelegatingCellViewModel {
    var delegate: CellDelegate? { get }
}

protocol ProductCellViewModelDelegate: CellDelegate {}

// Error: Type 'ProductCellViewModel' does not conform to protocol 'DelegatingCellViewModel'
class ProductCellViewModel: DelegatingCellViewModel {
    weak var delegate: ProductCellViewModelDelegate?
}

Full error message:

error: Playground.playground:9:7: error: type 'ProductCellViewModel' does not conform to protocol 'DelegatingCellViewModel'
class ProductCellViewModel: DelegatingCellViewModel {
      ^

Playground.playground:10:14: note: candidate has non-matching type 'ProductCellViewModelDelegate?'
    weak var delegate: ProductCellViewModelDelegate?
             ^

Playground.playground:4:9: note: protocol requires property 'delegate' with type 'CellDelegate?'; do you want to add a stub?
    var delegate: CellDelegate? { get }
        ^

Is this a language limitation or am I missing something? How should this code be written so it compiles and keeps the intent?

Nikita Kukushkin
  • 14,648
  • 4
  • 37
  • 45

1 Answers1

0

You need to change the type of delegate or add the protocol stubs.

class ProductCellViewModel: DelegatingCellViewModel {
    weak var delegate: CellDelegate?
}

OR you must change the name of that particular delegate like this. Because delegate name already used by conforming protocol -

class ProductCellViewModel: DelegatingCellViewModel {
    var delegate: CellDelegate?
    weak var productDelegate: ProductCellViewModelDelegate?
}
shivi_shub
  • 1,018
  • 1
  • 7
  • 15