Swift
isKindOfClass
programming
iOS development
Swift programming

Using isKindOfClass with Swift

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

swift
1import UIKit
2
3let label = UILabel()
4
5print(label.isKind(of: UIView.self))   // true
6print(label.isKind(of: UILabel.self))  // true
7print(label.isKind(of: UIButton.self)) // false

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:

swift
1import UIKit
2
3let view: UIView = UILabel()
4
5if view is UILabel {
6    print("This view is a UILabel")
7}

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:

swift
1import UIKit
2
3let view: UIView = UILabel()
4
5if let label = view as? UILabel {
6    label.text = "Updated"
7    print(label.text ?? "")
8}

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:

swift
1import UIKit
2
3let label = UILabel()
4
5print(type(of: label) == UILabel.self) // true
6print(type(of: label) == UIView.self)  // false

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:

swift
1import Foundation
2
3func describe(_ object: NSObject) {
4    if object.isKind(of: NSString.self) {
5        print("Objective-C string-compatible object")
6    }
7}
8
9describe("hello" as NSString)

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 is for 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, is is 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.'

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.