Create a NSObject class that introduces runtime
#import <objc/runtime.h>
. m file
/** * get all properties and corresponding values * */-(nsdictionary *) getallpropertiesandvalues{nsmutabledictionary *props = [Nsmutabledictionary di Ctionary]; unsigned int outcount; The list of attributes objc_property_t *properties =class_copypropertylist ([self class], &outcount); Traverse list for (int i = 0; i<outcount; i++) {objc_property_t = properties[i]; Gets the attribute string const char* propertyname =property_getname (property); Convert to nsstring nsstring *key = [NSString stringwithutf8string:propertyname]; Gets the property corresponding to the value ID of value = [self valueforkey:key]; if (value) {[props setobject:value forkey:key]; }}//release structure array memory free (properties); return props;} /** * Gets all properties of the object * * @return attribute array */-(Nsarray *) getallproperties{unsigned int count; Get the list of properties objc_property_t *properties =class_copypropertylist ([self class], &count); Nsmutablearray *propertiesarray = [Nsmutablearray arraywithcapacity:count]; for (int i = 0; i < count; i++) {objc_property_t = properties[i]; Const char* PropertyName =property_getname (property); [Propertiesarray addobject: [NSString Stringwithutf8string:propertyname]]; } free (properties); return Propertiesarray;} /** * Gets all the methods of the object */-(Nsarray *) getallmethods{unsigned int count_f = 0; Get method Chain list method* methodlist_f = Class_copymethodlist ([self class],&count_f); Nsmutablearray *methodsarray = [Nsmutablearray arraywithcapacity:count_f]; for (int i=0;i<count_f;i++) {Method temp_f = methodlist_f[i]; The call address of the method IMP Imp_f = Method_getimplementation (Temp_f); Method SEL Name_f = Method_getname (Temp_f); Method name string Const char* name_s =sel_getname (Method_getname (temp_f)); Number of parameters int arguments = method_getnumberofarguments (temp_f); The string that returns the description of the parameter and return value of the method is Const char* encoding =method_gettypeencoding (temp_f); NSLog (@ "method name:%@, Number of arguments:%d, encoding:%@", [NSString stringwithutf8string:name_s], arguments,[nsstring stringwithutf8s Tring:encoding]); NSString *methodstr = Nsstringfromselector (Name_f); [Methodsarray ADDOBJECT:METHODSTR]; } free (Methodlist_f); return Methodsarray;}
Runtime gets a list of object properties and methods