**
/* 注意:要先導入objectc執行時標頭檔案,以便呼叫runtime中的方法*/
#import
@implementation nsobject (propertylisting)
1、/* 獲取物件的所有屬性,不包括屬性值 */
- (nsarray *)getallproperties
u_int count;
objc_property_t *properties =class_copypropertylist([self class], &count);
nsmutablearray *propertiesarray = [nsmutablearray arraywithcapacity:count];
for (int i = 0; iconst char* propertyname =property_getname(properties[i]);
[propertiesarray addobject: [nsstring stringwithutf8string: propertyname]];
free(properties);
return propertiesarray;
2、/* 獲取物件的所有屬性 以及屬性值 */
- (nsdictionary *)properties_aps
nsmutabledictionary *props = [nsmutabledictionary dictionary];
unsigned int outcount, i;
objc_property_t *properties = class_copypropertylist([self class], &outcount);
for (i = 0; iobjc_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、/* 獲取物件的所有方法 */
-(void)printmothlist
unsigned int mothcout_f =0;
method* mothlist_f = class_copymethodlist([self class],&mothcout_f);
for(int i=0;imethod 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(@"方法名:%@,引數個數:%d,編碼方式:%@",[nsstring stringwithutf8string:name_s],
arguments,[nsstring stringwithutf8string:encoding]);
free(mothlist_f);
@end
IOS 遍歷未知物件的屬性和方法
注意 要先導入objectc執行時標頭檔案,以便呼叫runtime中的方法 import implementation nsobject propertylisting 1 獲取物件的所有屬性,不包括屬性值 nsarray getallproperties free properties retur...
IOS 遍歷未知物件的屬性和方法
注意 要先導入objectc執行時標頭檔案,以便呼叫runtime中的方法 import implementation nsobject propertylisting 1 獲取物件的所有屬性,不包括屬性值 nsarray getallproperties free properties retur...
IOS 遍歷未知物件的屬性和方法
注意 要先導入objectc執行時標頭檔案,以便呼叫runtime中的方法 import implementation nsobject propertylisting 1 獲取物件的所有屬性,不包括屬性值 nsarray getallproperties free properties retur...