IOS traversal properties and methods of unknown objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.