Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

swift - List all subclasses of one class

Can I return a list of all subclasses of one class? For example:

class Mother {

}

class ChildFoo: Mother {

}

class ChildBar: Mother {

}

let motherSubclasses = ... // TODO
print(motherSubclasses) // should to return [ChildFoo.self, ChildBar.self]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...