获取某个类的所有属性,这里举例UIAlertAction
extension UIAlertAction {
/// 取属性列表
static var propertyNames: [String] {
var outCount: UInt32 = 0
guard let ivars = class_copyIvarList(self, &outCount) else {
return []
}
var result = [String]()
let count = Int(outCount)
for i in 0..<count {
guard let pro: Ivar = ivars[i] else {
continue
}
guard let name = String(utf8String: ivar_getName(pro)) else {
continue
}
result.append(name)
}
return result
}
}
调用下面方法打印出属性列表
dump(UIAlertAction.propertyNames)