Sorry for the basic question. I am working through the Head First Swift book and have hit a snag. I am trying to iterate over an array of custom types and access the functions of the Dog and Cat types in a for in loop but I can't figure out the syntax. How do I call either bark() or meow() depending on whether the item in the array is a Dog or Cat?
protocol Animal {
var type: String { get }
}
struct Dog: Animal {
var name: String
var type: String
func bark() {
print("Woof woof")
}
}
struct Cat: Animal {
var name: String
var type: String
func meow() {
print("Meow")
}
}
var bunty = Cat(name: "Bunty", type: "British Shorthair")
var nigel = Cat(name: "Nigel", type: "Russian Blue")
var percy = Cat(name: "Percy", type: "Manx")
var argos = Dog(name: "Argos", type: "Whippet")
var barny = Dog(name: "Barny", type: "Bulldog")
var animals: [Animal] = [bunty, nigel, percy, argos, barny]
print(animals.count)
for animal in animals {
}
I have tried an if statement in the loop:
for animal in animals {
if animal == Cat {
meow()
} else {
bark()
}
}
But Swift says "Binary operator == cannot be applied to operands Animal and Cat.type. Thanks for your help, I'm trying to learn.