引用AutoreleasingUnsafeMutablePointer

时间:2016-05-17 02:44:11

标签: swift pointers

我想用Objective-C运行时函数objc_copyClassList()获取所有已知类的列表:

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

count返回类似3900的内容。classListAutoreleasingUnsafeMutablePointer<Optional<AnyObject.Type>>类型的对象。

如何遍历classList以获取class_getName()所有3900个班级的名称?

1 个答案:

答案 0 :(得分:1)

好的,我自己弄清楚了:

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

for i in 0..<Int(count) {
    let cName = class_getName(classList[i])
    let classname = NSString(CString: cName, encoding: NSUTF8StringEncoding)!
    print(className)
}