Get all attributes of an object & get all methods of an object

Source: Internet
Author: User

# Import <objc/runtime. h>

 

 

@ Implementation nsobject (propertylisting)

 

/* Obtain all attributes of an object */

-(Nsdictionary *) properties_aps

{

Nsmutabledictionary * props = [nsmutabledictionarydictionary];

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 = [nsstringstringwithuf8string: char_f];

Id propertyvalue = [selfvalueforkey :( nsstring *) propertyname];

If (propertyvalue) [props setobject: propertyvalue forkey: propertyname];

}

Free (properties );

Return props;

}

 

/* Get all 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 parameters: % d, encoding method: % @", [nsstringstringwithuf8string: name_s],

Arguments,

[Nsstringstringwithuf8string: encoding]);

}

Free (mothlist_f );

}

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.