一、创建NSObject的Category文件。引入runtime头文件。
Runtime各种方法属性参见:总结iOS Runtime中使用的函数_Holothurian的博客-CSDN博客
#import <objc/runtime.h>
//获取对象的所有属性
+ (NSArray *)getAllProperties{
u_int count = 0;
//传递count的地址
objc_property_t *properties = class_copyPropertyList([self class], &count);
NSMutableArray *propertyArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count; i++) {
//得到的propertyName为C语言的字符串
const char *propertyName = property_getName(properties[i]);
[propertyArray addObject:[NSString stringWithUTF8String:propertyName]];
// NSLog(@"%@",[NSString stringWithUTF8String:propertyName]);
}
free(properties);
return propertyArray;
}
//获取对象的所有方法
+ (NSArray *)getAllMethods{
unsigned int methodCount = 0;
Method *methodList = class_copyMethodList([self class], &methodCount);
NSMutableArray *methodArray = [NSMutableArray arrayWithCapacity:methodCount];
for (int i = 0; i < methodCount; i++) {
Method temp = methodList[i];
SEL name_F = method_getName(temp);
const char *name_s = sel_getName(name_F);
int arguments = method_getNumberOfArguments(temp);
const char * encoding = method_getTypeEncoding(temp);
// NSLog(@"MethodName: %@,ArgumentCount: %d,EncodingStyle: %@",[NSString stringWithUTF8String:name_s],arguments,[NSString stringWithUTF8String:encoding]);
[methodArray addObject:[NSString stringWithUTF8String:name_s]];
}
free(methodList);
return methodArray;
}
//调用
NSLog(@"%@",[UITextField getAllProperties]);
————————————————
版权声明:本文为CSDN博主「Holothurian」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sharktoping/article/details/78501166
————————————————
另参考文章: