Ext.: http://blog.csdn.net/crazychickone/article/details/36413671
/* Note: To first import the OBJECTC run-time header file in order to invoke the method in runtime */
#import <objc/runtime.h>
@implementation NSObject (propertylisting)
1./* Gets all properties of the object, excluding property values */
-(Nsarray *) getallproperties
{
U_int count;
objc_property_t *properties =class_copypropertylist ([self class], &count);
Nsmutablearray *propertiesarray = [Nsmutablearray arraywithcapacity:count];
for (int i = 0; i<count; i++)
{
Const char* PropertyName =property_getname (Properties[i]);
[Propertiesarray addobject: [NSString Stringwithutf8string:propertyname]];
}
Free (properties);
return propertiesarray;
}
2./* Gets all properties and property values of the object */
-(Nsdictionary *) properties_aps
{
Nsmutabledictionary *props = [Nsmutabledictionary dictionary];
unsigned int outcount, I;
objc_property_t *properties = Class_copypropertylist ([self class], &outcount);
for (i = 0; i<outcount; i++)
{
objc_property_t property = Properties[i];
Const char* Char_f =property_getname (property);
NSString *propertyname = [NSString stringwithutf8string:char_f];
ID propertyvalue = [self Valueforkey: (NSString *) PropertyName];
if (PropertyValue) [props setobject:propertyvalue forkey:propertyname];
}
Free (properties);
return props;
}
3./* Get all the methods of the object */
-(void) printmothlist
{
unsigned int mothcout_f = 0;
method* mothlist_f = Class_copymethodlist ([self class],&mothcout_f);
for (int i=0;i<mothcout_f;i++)
{
Method temp_f = Mothlist_f[i];
IMP imp_f = method_getimplementation (Temp_f);
SEL Name_f = Method_getname (Temp_f);
Const char* name_s =sel_getname (Method_getname (temp_f));
int arguments = method_getnumberofarguments (temp_f);
Const char* Encoding =method_gettypeencoding (temp_f);
NSLog (@ "method name:%@, Number of arguments:%d, encoding:%@", [NSString stringwithutf8string:name_s],
Arguments,[nsstring stringwithutf8string:encoding]);
}
Free (mothlist_f);
}
@end
IOS traversal properties and methods of unknown objects