What does this print?
class Animal {}
class Dog: Animal {}
let items: [Animal] = [
Dog(), Animal(), Dog()
]
var count = 0
for item in items {
if item is Animal { count += 1 }
}
print(count)
Try it in the online Swift Playground →
[spoiler title="Solution"]
Answer:
3
Explanation:
The is operator checks runtime type; all three elements are Animal instances as Dog is a subclass.
[/spoiler]