I've been playing around with Swift protocols and I'm trying to figure out why this code isn't working...
protocol Animal {
var name: String {get}
var breed: String {get}
}
struct Bird: Animal {
var name: String
var breed: String
var wingspan: Double
}
protocol AnimalHouse {
var myAnimal: Animal! {get set}
}
class Birdhouse: AnimalHouse {
var myAnimal: Bird!
func isOpeningBigEnough() -> Bool {
return myAnimal.wingspan <= 5.0
}
}
The problem the compiler keeps giving me is that that BirdHouse
doesn't conform to protocol AnimalHouse
. If you follow up, it'll tell you that myAnimal
requires type Animal
, and I'm supplying type Bird
. Obviously, Bird
does conform to the Animal
protocol, but that's not enough to make the compiler happy.
I'm assuming this is one of those one-line fixes where the trick is knowing where the one line is. Anybody have any suggestions?
(And, yes, I could make myAnimal
an Animal
and then cast it as a Bird
later in the function, but that seems unnecessarily messy.)