iOS ● ● ○ Swift 6

Kodebits Day 47: Type Casting Check

Jun 26 2026
Practice type casting with a short swift challenge.

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]


Further Reading