OC reflection -- & gt; dynamically create classes, oc --

Source: Internet
Author: User

OC reflection --> dynamic creation class, oc --

 

Create a Class
1-(Class) createNewClass {2 const char * className; 3 className = [@ "Student" UTF8String]; 4 Class kclass = objc_getClass (className ); 5 // determine whether this class already exists. if it exists, it will return. if it does not exist, it will create 6 if (! Kclass) 7 {8 Class superClass = [NSObject class]; 9 kclass = objc_allocateClassPair (superClass, className, 0); 10} 11 return kclass; 12}

 

 

 

 

Add member variables
1 // Add member variable 2-(void) addNewVariable :( Class) newClass {3 class_addIvar (newClass, "_ stuName", sizeof (NSString *), 0 ,"@"); 4}

 

 

Obtains the member variable name of a Class.
1 // get the member variable name of the class 2-(NSArray *) getVariableNamesByObject :( id) object 3 {4 unsigned int numIvars = 0; 5 // obtain all the member variables of the class 6 Ivar * ivars = class_copyIvarList ([object class], & numIvars ); 7 // define an array to receive the obtained attribute name 8 NSMutableArray * nameArray = [[NSMutableArray alloc] initWithCapacity: numIvars]; 9 for (int I = 0; I <numIvars; I ++) {10 // obtain a single member variable 11 Ivar thisIvar = ivars [I]; 12 // obtain the member variable type 13 const char * type = Ivar_getTypeEncoding (thisIvar); 14 NSString * stringType = [NSString stringWithCString: type encoding: NSUTF8StringEncoding]; 15 // skip 16 if (! [StringType hasPrefix: @ "@"]) {17 continue; 18} 19 // get the member variable name 20 NSString * variableName = [NSString stringwithuf8string: ivar_getName (thisIvar)]; 21 [nameArray addObject: variableName]; 22 23 // The value of the member variable is 24 // object_getIvar (object, thisIvar) 25 26} 27 free (ivars ); 28 return nameArray; 29}

 

 

After the call, the result is:

 

 

 

 

Creation Method

The first parameter is the class name, the second parameter is the method name, the third parameter is the function name, the fourth parameter is the function return value and parameter type, and the v Table is void, @ indicates id and SEL. For more definitions, see SELECTOR.

1    class_addMethod([kclass class], @selector(say:), (IMP)say, "v@:");

 

 

Implementation is required. This method

 

 

 

 

 

Add attribute
1 // Add an attribute 2-(void) addProperties :( Class) newClass {3 NSString * propertyName = @ "stuSex"; 4 objc_property_attribute_t type = {"T ", "@ \" NSString \ ""}; 5 objc_property_attribute_t ownership = {"C", "copy"}; 6 objc_property_attribute_t backingivar = {"V", [propertyName UTF8String]}; 7 objc_property_attribute_t attrs [] = {type, ownership, backingivar}; 8 BOOL isOk = class_addProperty (newClass, [propertyName UTF8String], attrs, 3); 9}

 

 

View attributes
1 // get all attribute names of the class 2-(NSArray *) getPropertieNamesByObject :( id) object 3 {4 5 unsigned int outCount, I; 6 7 // get the attribute list of the registration class, the first parameter is the class, and the second parameter is the variable 8 objc_property_t * properties = class_copyPropertyList ([object class], & outCount) for receiving the number of class attributes ); 9 // define an array to receive the obtained attribute name 10 NSMutableArray * nameArray = [[NSMutableArray alloc] initWithCapacity: outCount]; 11 for (I = 0; I <outCount; I ++) {12 // obtain a single attribute through a loop 13 objc_property_t property = properties [I]; 14 // obtain the attribute name 15 NSString * propertyName = [[NSString alloc] initWithCString: property_getName (property) encoding: NSUTF8StringEncoding]; 16 // Add the obtained attribute name to the array 17 [nameArray addObject: propertyName]; 18 19} 20 free (properties); 21 return nameArray; 22}

 

 

Print result:

 

 

Related Article

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.