Using isKindOfClass with Swift
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Swift, the idea behind Objective-C isKindOfClass appears as isKind(of:) on NSObject types, but in most Swift code you should prefer native operators such as is and as?. The key is understanding what kind of type check you need: subclass-compatible, exact-type, or safe downcast.
What isKind(of:) Means
isKind(of:) answers this question: is this object an instance of the given class or any subclass of it? That is why a UILabel counts as a kind of UIView.
This method comes from the Objective-C runtime and is available because UIKit classes inherit from NSObject.
The Native Swift Alternative: is
In pure Swift code, is is usually the better choice because it is more idiomatic and easier to read:
This check also respects subclassing, so it answers the same logical question in many day-to-day cases.
Safe Downcasting with as?
Often you do not just want to know the type. You want to use the subtype’s API. In that case, as? is better than a standalone check:
This combines the type check with a safe cast and avoids doing the same reasoning twice.
Exact Type Checks Are Different
Sometimes you want to know whether the object is exactly one class, not a subclass. isKind(of:) is not for that. Use type(of:) equality instead:
That distinction matters in code that behaves differently for subclasses.
When isKind(of:) Still Makes Sense
isKind(of:) is most relevant when:
- you are working with Objective-C based frameworks
- you are calling APIs that return
AnyObject - you are maintaining mixed Swift and Objective-C code
For example:
In cases like this, the Objective-C runtime model is already part of the API, so isKind(of:) is not out of place.
Choosing the Right Tool
A practical rule is:
- use
isfor a simple type check in Swift - use
as?when you need to cast and then use subtype behavior - use
type(of:) ==for exact-type comparison - use
isKind(of:)when Objective-C interop is already part of the design
That keeps the code idiomatic while still respecting framework reality.
Common Pitfalls
One common mistake is using isKind(of:) everywhere just because it exists. In modern Swift, that usually makes the code feel more Objective-C-flavored than necessary.
Another pitfall is confusing subclass checks with exact-type checks. UILabel is a kind of UIView, but it is not exactly the same type as UIView. If that distinction matters, use type(of:).
Developers also sometimes perform a type check and then force-cast with as!. If the goal is safe code, as? with optional binding is almost always the better choice.
Finally, remember that isKind(of:) works on class types, not value types. Structs and enums use Swift’s native type system instead.
Summary
- '
isKind(of:)checks whether an object is an instance of a class or one of its subclasses.' - In Swift,
isis usually the cleaner and more idiomatic alternative for simple checks. - Use
as?when you want both a type check and a safe cast. - Use
type(of:) ==if you need an exact-type comparison instead of subclass compatibility. - '
isKind(of:)is most useful in mixed Swift and Objective-C codebases.'

