首页 > 解决方案 > 为什么 Swift 不认为协议采用者属性是一致的?

问题描述

protocol Animal {}
struct Lion: Animal {}
protocol Zoo {
    var animals: [Animal] { get }
}
struct BronxZoo: Zoo { // error: Type 'BronxZoo' does not conform to protocol 'Zoo'
    var animals: [Lion] = []
}

如果BronxZoo有一个animals类型为 的数组Lion,它符合Animal,那么为什么不BronxZoo被认为符合Zoo

标签: swift

解决方案


推荐阅读