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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
@KamilHarasimowicz I think, yes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.