Runtime gets a list of object properties and methods

Source: Internet
Author: User
Tags list of attributes

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

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.