1

I am a bit confused. Please look at this example.

I created a VM protocol:

protocol VM {

}

And this protocol is using in my VC implementation

final class VC: UIViewController {
    let viewModel: VM
}

Now I create special new protocols

protocol AwesomeProtocol {

}

protocol AwesomeViewProtocol {
     var viewModel: AwesomeProtocol { get }
}

My idea is to expand VM with Awesomeness so:

protocol VM: AwesomeProtocol {

}

final class VC: UIViewController, AwesomeViewProtocol {
    let viewModel: VM
}

But here I met an compiler error:

Type 'VC' does not conform to protocol 'AwesomeViewProtocol'

Despite the fact that VM extend AwesomeProtocol

Someone could explain me what am I doing wrong?

Kamil Harasimowicz
  • 4,684
  • 5
  • 32
  • 58

1 Answers1

1

You have to implement this.

final class VC: UIViewController, AwesomeViewProtocol {
    var viewModel: AwesomeProtocol
}

computed variables are close to the functions. Their signatures must be the same in a parent and child (inherited) classes/protocols.

If you need something abstracts use assosiatedtype and generic classes instead.

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • In your implementation `VC` lose access to `VM` methods. It will can use only `AwesomeProtocol` interface. According to your answer I assume that my implementation idea is not possible and the only case is to use mentioned `assosiatedtype`. Am I right? – Kamil Harasimowicz May 16 '18 at 10:15
  • @KamilHarasimowicz I think, yes – Vyacheslav May 16 '18 at 10:16