0
在Swift中,当我遇到一个问题,它不能编译,因为它声称“父不符合BaseParent”,但在Prototype中尝试了一些不同的东西,即使我不能为我的生活理解为什么。可能是一个返朴归真的时刻...... 不管怎么说,这是一个解释,代码:Swift Protcol继承不编译
/*
- 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!
}
可能关于我失踪,但如果任何人都可以进入更多的细节我会很感激的协议一些显而易见的原因它:)
吸气剂OK,你正在返回符合'BaseChild'协议的对象,但没有按二传手不匹配。你不能在'BaseParent'中设置任何符合'BaseChild'的对象,它期望任何符合'Child'协议的对象。 – redent84
当然有道理。添加它作为答案,当它让我时,我会接受它。 – SeanCAtkinson