-(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