oc写法
//获取指定类的子类
- (NSArray *)findSubClass:(Class)defaultClass
{
//注册类的总数
int count = objc_getClassList(NULL,0);
//创建一个数组,其中包含给定对象
NSMutableArray * array = [NSMutableArray arrayWithObject:defaultClass];
//获取所有已注册的类
Class *classes = (Class *)malloc(sizeof(Class) * count);
objc_getClassList(classes, count);
//遍历
for (int i = 0; i < count; i++) {
if (defaultClass == class_getSuperclass(classes[i])) {
[array addObject:classes[i]];
}
}
free(classes);
return array;
}
swift 写法
fileprivate class func performOnce(){
//注册类的总数
let count = Int(objc_getClassList(nil, 0))
//分配空间
let classes = UnsafeMutablePointer<AnyClass?>.allocate(capacity: count)
//转类型
let autoreleaseClasses = AutoreleasingUnsafeMutablePointer<AnyClass>(classes)
//获取所有的类
objc_getClassList(autoreleaseClasses, Int32(count))
for index in 0..<count{
(classes[index] as? AwakeProtocol.Type)?.awake()
}
//释放
classes.deallocate()
}