Was trying out some different things with Protocl Inheritance in swift when I came upon an issue where it doesn't compile as it claims "Parent does not conform to BaseParent" even though I can't for the life of me understand why. Probably a back to basics moment... Anyway, here is an explanation and the code:
/*
- To conform to 'BaseParent', you must have a var called 'child' that conforms to 'BaseChild'
- 'Child' conforms to 'BaseChild'
- 'Parent' has a property called 'child' which must conform to 'Child' and by extension, 'BaseChild'
- Therefore, any element assigned to 'child', must conform to 'BaseChild'
- Therefore, 'Parent' has a var called 'child' that conforms to 'BaseChild'
- So why does 'Parent' not conform to 'BaseParent'
*/
protocol BaseChild { /* some methods */ }
protocol Child : BaseChild { /* some more methods */ }
protocol BaseParent {
var child : BaseChild! {get set}
}
class Parent : BaseParent {
var child : Child!
}
Probably some obvious reason about protocols that I'm missing but if anyone can go into more details I'd appreciate it :)
BaseChildprotocol, but the setter doesn't match. You cannot set any object conformingBaseChildas expected inBaseParent, it expects any object conforming toChildprotocol.