可利用反射机制获取某个类的属性及方法列表,代码很简单,重点用到class_copyPropertyList和class_copyMethodList,如下:
extension NSObject {
func getPropertyNames(){
var outCount:UInt32
outCount = 0
let propers:UnsafeMutablePointer<objc_property_t>! = class_copyPropertyList(self.classForCoder, &outCount)
let count:Int = Int(outCount);
print(outCount)
for i in 0...(count-1) {
let aPro: objc_property_t = propers[i]
let proName:String! = String(UTF8String: property_getName(aPro));
print(proName)
}
}
func getMethodNames(){
var outCount:UInt32
outCount = 0
let methods:UnsafeMutablePointer<objc_property_t>! = class_copyMethodList(self.classForCoder, &outCount)
let count:Int = Int(outCount);
print(outCount)
for i in 0...(count-1) {
let aMet: objc_property_t = methods[i]
let methodName:String! = String(UTF8String: property_getName(aMet));
print(methodName)
}
}
}