Checking Types with is
Test an instance's type before casting.
Checking Types with is is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The is Operator
The type-check operator is returns a Bool telling you whether an instance is of a given type (or subtype). It does not cast; it just tests.
class Animal {
let name: String
init(name: String) { self.name = name }
}
class Dog: Animal {
func bark() -> String { return name + " says Woof" }
}
class Cat: Animal {
func meow() -> String { return name + " says Meow" }
}
let a: Animal = Dog(name: "Rex")
print(a is Dog)
print(a is Cat)Testing Subtype Membership
is is true when the instance is exactly that type or any subclass of it. A Dog is an Animal.
class Animal {
let name: String
init(name: String) { self.name = name }
}
class Dog: Animal {
func bark() -> String { return name + " says Woof" }
}
class Cat: Animal {
func meow() -> String { return name + " says Meow" }
}
let a: Animal = Dog(name: "Rex")
print(a is Animal)
print(a is Dog)Using is in Conditions
You can branch on a type check with if, choosing behavior based on the runtime type.
class Animal {
let name: String
init(name: String) { self.name = name }
}
class Dog: Animal {
func bark() -> String { return name + " says Woof" }
}
class Cat: Animal {
func meow() -> String { return name + " says Meow" }
}
let a: Animal = Cat(name: "Mia")
if a is Cat {
print("It is a cat")
} else {
print("Not a cat")
}Counting Types in a Collection
A handy use of is is counting how many elements match a type within a mixed array.
class Animal {
let name: String
init(name: String) { self.name = name }
}
class Dog: Animal {
func bark() -> String { return name + " says Woof" }
}
class Cat: Animal {
func meow() -> String { return name + " says Meow" }
}
let zoo: [Animal] = [Dog(name: "Rex"), Cat(name: "Mia"), Dog(name: "Buddy")]
let dogCount = zoo.filter { $0 is Dog }.count
print("Dogs:", dogCount)is with Protocols
is also tests whether an instance conforms to a protocol, returning true when it does.
protocol Flyer {}
class Bird: Flyer {}
class Fish {}
let creatures: [Any] = [Bird(), Fish()]
for c in creatures { print(c is Flyer) }is vs as?
is only answers yes or no. as? goes further and gives you the value if you need to use the subclass members.
class Animal {
let name: String
init(name: String) { self.name = name }
}
class Dog: Animal {
func bark() -> String { return name + " says Woof" }
}
class Cat: Animal {
func meow() -> String { return name + " says Meow" }
}
let a: Animal = Dog(name: "Rex")
print("Check only:", a is Dog)
if let d = a as? Dog { print("Use it:", d.bark()) }Guarding with is
Use is in a guard to bail out early when an instance is not the expected type.
class Animal {
let name: String
init(name: String) { self.name = name }
}
class Dog: Animal {
func bark() -> String { return name + " says Woof" }
}
class Cat: Animal {
func meow() -> String { return name + " says Meow" }
}
func onlyForCats(_ a: Animal) {
guard a is Cat else {
print("Skipping non-cat")
return
}
print("Processing a cat")
}
onlyForCats(Cat(name: "Mia"))
onlyForCats(Dog(name: "Rex"))Combining is in switch
You can switch over type checks indirectly, but pattern matching with case is reads cleanly when handling several types.
class Animal {
let name: String
init(name: String) { self.name = name }
}
class Dog: Animal {
func bark() -> String { return name + " says Woof" }
}
class Cat: Animal {
func meow() -> String { return name + " says Meow" }
}
let items: [Any] = [Dog(name: "Rex"), 42, "hello"]
for item in items {
switch item {
case is Dog: print("a dog")
case is Int: print("an integer")
default: print("something else")
}
}is Checks the Runtime Type
is inspects the actual runtime type, not the declared variable type. A variable typed Animal holding a Dog passes is Dog.
class Animal {
let name: String
init(name: String) { self.name = name }
}
class Dog: Animal {
func bark() -> String { return name + " says Woof" }
}
class Cat: Animal {
func meow() -> String { return name + " says Meow" }
}
let declaredAsAnimal: Animal = Dog(name: "Rex")
print("Runtime is Dog:", declaredAsAnimal is Dog)Validating Before a Forced Cast
A safe pattern is checking with is and only then using as!, because you have proven the type.
class Animal {
let name: String
init(name: String) { self.name = name }
}
class Dog: Animal {
func bark() -> String { return name + " says Woof" }
}
class Cat: Animal {
func meow() -> String { return name + " says Meow" }
}
let a: Animal = Dog(name: "Rex")
if a is Dog {
let d = a as! Dog
print(d.bark())
}Type Checking Summary Example
Together, is for checking and as? for casting give you a complete, safe toolkit for runtime types.
class Animal {
let name: String
init(name: String) { self.name = name }
}
class Dog: Animal {
func bark() -> String { return name + " says Woof" }
}
class Cat: Animal {
func meow() -> String { return name + " says Meow" }
}
let zoo: [Animal] = [Dog(name: "Rex"), Cat(name: "Mia")]
for a in zoo {
if a is Dog { print("dog:", a.name) }
if a is Cat { print("cat:", a.name) }
}Quick Check
Recall what the is operator returns.
Recap: Checking Types with is
You learned that is returns a Bool for runtime type membership including subclasses and protocol conformance, that it pairs well with filtering and switch case is, and that checking with is before an as! makes a forced cast safe.
class Animal {
let name: String
init(name: String) { self.name = name }
}
class Dog: Animal {
func bark() -> String { return name + " says Woof" }
}
class Cat: Animal {
func meow() -> String { return name + " says Meow" }
}
let zoo: [Animal] = [Dog(name: "Rex"), Cat(name: "Mia"), Dog(name: "Buddy")]
print("Cats:", zoo.filter { $0 is Cat }.count)
print("Dogs:", zoo.filter { $0 is Dog }.count)Frequently asked questions
Is the “Checking Types with is” lesson free?
Yes — the full text of “Checking Types with is” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Checking Types with is”?
Test an instance's type before casting. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Checking Types with is” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.