List all subclasses of one class

Solutions Collect From Internet About “List all subclasses of one class”

Surprisingly the Objective-C runtime functions work just as well with Swift classes, even if they don’t subclass from NSObject. Further, all classes in Swift seems to derive from SwiftObject. SwiftObject itself does not have a superclass.

First, a wrapper structure to handle the ObjC runtime functions:

import Foundation

struct ClassInfo : CustomStringConvertible, Equatable {
    let classObject: AnyClass
    let className: String

    init?(_ classObject: AnyClass?) {
        guard classObject != nil else { return nil }

        self.classObject = classObject!

        let cName = class_getName(classObject)!
        self.className = String(cString: cName)
    }

    var superclassInfo: ClassInfo? {
        let superclassObject: AnyClass? = class_getSuperclass(self.classObject)
        return ClassInfo(superclassObject)
    }

    var description: String {
        return self.className
    }

    static func ==(lhs: ClassInfo, rhs: ClassInfo) -> Bool {
        return lhs.className == rhs.className
    }
}

Here’s how you can use it:

class Mother { }
class ChildFoo: Mother { }
class ChildBar: Mother { }

class AnIrrelevantClass { }

let motherClassInfo = ClassInfo(Mother.self)!
var subclassList = [ClassInfo]()

var count = UInt32(0)
let classList = objc_copyClassList(&count)!

for i in 0..<Int(count) {
    if let classInfo = ClassInfo(classList[i]),
        let superclassInfo = classInfo.superclassInfo,
        superclassInfo == motherClassInfo
    {
        subclassList.append(classInfo)
    }
}

print(subclassList)

This only performs a shallow search so it won’t sweep up grandchildren classes but you get the idea.