Traversing class members

Source: Internet
Author: User

-(Nsdictionary *) todictionary{    nsmutabledictionary *dictionaryformat = [nsmutabledictionary dictionary];  Gets the current class type class    CLS = [self class];        unsigned int ivarscnt = 0;    Gets a list of class member variables, ivarscnt for the number of class members    Ivar *ivars = class_copyivarlist (CLS, &ivarscnt);        Iterate through the list of member variables, where each variable is a struct of type Ivar for    (const Ivar *p = Ivars; p < Ivars + ivarscnt; ++p)    {        Ivar const IVAR = *p;                Gets the variable name        nsstring *key = [NSString stringwithutf8string:ivar_getname (Ivar)];        If this variable is not declared in the class struct and declared as property only, then the variable name is prefixed with ' _ ' underline        //such as @property (retain) nsstring *abc; then key = = _ABC;                Gets the variable value ID of value        = [self valueforkey:key];                Gets the variable type        //through TYPE[0] can judge its specific built-in type        const char *type = ivar_gettypeencoding (Ivar);                if (value)        {            [Dictionaryformat setobject:value forkey:key];        }     }    return Dictionaryformat;}

Traversing class members

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.