列出一个类的所有子类

时间:2017-03-12 11:44:00

标签: swift reflection

我可以返回一个类的所有子类的列表吗?例如:

.catch(error => {
    dispatch({
        type: authActions.AUTH_PROCESS_ERROR,
        error: error.response ? error.response.data.code.toString() : 'Something went wrong, please try again.'
    }); 
});

4 个答案:

答案 0 :(得分:8)

令人惊讶的是,Objective-C运行时函数与Swift类一样工作,即使它们不是NSObject的子类。此外,Swift中的所有类似乎都来自SwiftObjectSwiftObject本身没有超类。

首先,处理ObjC运行时函数的包装器结构:

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
    }
}

以下是如何使用它:

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)

这只执行浅层搜索,因此它不会扫除孙子类,但你明白了。

答案 1 :(得分:2)

这是基于Code Different之前的答案的变体,但更简短:

func subclasses<T>(of theClass: T) -> [T] {
    var count: UInt32 = 0, result: [T] = []
    let allClasses = objc_copyClassList(&count)!

    for n in 0 ..< count {
        let someClass: AnyClass = allClasses[Int(n)]
        guard let someSuperClass = class_getSuperclass(someClass), String(describing: someSuperClass) == String(describing: theClass) else { continue }
        result.append(someClass as! T)
    }

    return result
}

由于泛型,返回类型将根据接收变量的类型。

我想将此代码编写为AnyClass的扩展...但是不幸的是Swift不允许这样做。

答案 2 :(得分:2)

Jean Le Moignan代码的优化版本

    static func subclasses<T>(of theClass: T) -> [T] {
        var count: UInt32 = 0, result: [T] = []
        let allClasses = objc_copyClassList(&count)!
        let classPtr = address(of: theClass)

        for n in 0 ..< count {
            let someClass: AnyClass = allClasses[Int(n)]
            guard let someSuperClass = class_getSuperclass(someClass), address(of: someSuperClass) == classPtr else { continue }
            result.append(someClass as! T)
        }

        return result
    }
public func address(of object: Any?) -> UnsafeMutableRawPointer{
    return Unmanaged.passUnretained(object as AnyObject).toOpaque()
}

对于每种类型,在运行时只有一个元类实例,因此,它们上的指针是唯一的。 出于某种原因,AnyClass不允许使用运算符===,但是我们可以直接比较指针

性能测试:

        let start = CFAbsoluteTimeGetCurrent()
        let found = RuntimeUtils.subclasses(of:UIViewController.self)
        let diff = CFAbsoluteTimeGetCurrent() - start
        print("Took \(diff) seconds, \(found.count) found")

输出:

String(描述:theClass): Took 1.0465459823608398 seconds, 174 found

地址(of:theClass): Took 0.2642860412597656 seconds, 174 found

答案 3 :(得分:0)

谢谢你让。这就是与所述FUNC亚类(theClass描述的:T)非常有帮助

let answer = subclasses(of: CIFilter.self) 

answer在iOS 12.1.3中具有282个CIFilter子类。这比文档页面中列出的要多得多