What the reflection mechanism can get
1. Get all the properties of a class
1 #pragma-mark getting a list of variables2 3-(void) Getvarname4 {5UnsignedintNumivars;//number of member variables6Ivar *vars = class_copyivarlist (nsclassfromstring (@"Testobject"), &numivars);7 //Ivar *vars = Class_copyivarlist ([UIView class], &numivars);8 9NSString *key=Nil;Ten for(inti =0; i < Numivars; i++) { One AIvar Thisivar =Vars[i]; -key = [NSString Stringwithutf8string:ivar_getname (Thisivar)];//gets the name of the member variable -NSLog (@"variable name:%@", key); thekey = [NSString stringwithutf8string:ivar_gettypeencoding (Thisivar)];//get the data type of a member variable -NSLog (@"variable type:%@", key); - } - Free (VARs); + -}
2. Get all the methods of a class:
1 #pragma-mark get a list of methods2 3-(void) Getfunname4 {5UnsignedintNumivars;//number of member methods6Method *meth = class_copymethodlist (nsclassfromstring (@"Testobject"), &numivars);7 //Method *meth = Class_copymethodlist ([UIView class], &numivars);8 9 for(inti =0; i < Numivars; i++) {TenMethod Thisivar =Meth[i]; One ASel sel =Method_getname (Thisivar); - Const Char*name =sel_getname (SEL); - theNSLog (@"ZP method:%s", name); - - - } + Free (meth); -}
3. Get the variable name
1 #pragma-mark Get property names based on variable property values2 3-(NSString *) Namewithinstance: (ID) Instance4 {5UnsignedintNumivars =0;6NSString *key=Nil;7Ivar * Ivars = class_copyivarlist ([testobjectclass], &numivars);8 for(inti =0; i < Numivars; i++) {9Ivar Thisivar =Ivars[i];Ten Const Char*type =ivar_gettypeencoding (Thisivar); OneNSString *stringtype =[NSString Stringwithcstring:type encoding:nsutf8stringencoding]; A if(! [StringType Hasprefix:@"@"]) { - Continue; - } the if((Object_getivar (TObject, thisivar) = =instance)) { -Key =[NSString Stringwithutf8string:ivar_getname (Thisivar)]; - Break; - } + } - Free (ivars); + ANSLog (@"key:%@", key); at - returnkey; - -}
iOS Development Reflection (ii)