Duško Mirković
Duško Mirković

Reputation: 321

Check if class is a subclass of another class

How can I check if a class is a subclass of another class using types only, without objects?

Something like:

class SuperClass {}
class SubClass: SuperClass {}

SuperClass.self == SubClass.self // should return true, but it returns false

Upvotes: 1

Views: 724

Answers (1)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119128

Your classes should inherit from the NSObject. Then you can check that like:

import Foundation

class SuperClass: NSObject { }
class SubClass: SuperClass { }

SubClass.isSubclass(of: SuperClass.self) // Returns true

Upvotes: 2

Related Questions