The dynamic creation of Class

A. uses objc_allocateClassPair to create a Class class
const char * className = "Calculator";
Class kclass = objc_getClass(className);
if (!kclass)
Class superClass = [NSObject class];
kclass = objc_allocateClassPair(superClass, className, 0);

B. use class_addIvar to add a member variable
NSUInteger size;
NSUInteger alignment;
NSGetSizeAndAlignment("*", &size, &alignment);
class_addIvar(kclass, "expression", size, alignment, "*");

Definition: tTypeEncodings.html 1.type reference...
2."*"The asterisk character (iOS), the character of 4 bits, and uses 4 bit aligned kclass

C. use class_addMethod to add member method
class_addMethod(kclass, @selector(setExpressionFormula:), (IMP)setExpressionFormula, "v@:@");
class_addMethod(kclass, @selector(getExpressionFormula), (IMP)getExpressionFormula, "@@:");

static void setExpressionFormula(id self, SEL cmd, id value)
NSLog(@"call setExpressionFormula");

static void getExpressionFormula(id self, SEL cmd)
NSLog(@"call getExpressionFormula");

Definition: tTypeEncodings.html 1.type reference...
2."v@:@",Explain the v- return value of type void, type ID @-self pointer, pointer types: -SEL SEL, the first parameter is of type ID @ - function
3."@@:",Explain the @ - return values of type ID, type ID @-self pointer, pointer type: -SEL SEL,

D. is registered to the runtime environment
The E. class is instantiated
id instance = [[kclass alloc] init];
F. to assign values to variables
object_setInstanceVariable(instance, "expression", "1+1");
G. gets the variable value
void * value = NULL;
object_getInstanceVariable(instance, "expression", &value);
H. function call
[instance performSelector:@selector(getExpressionFormula)];

Started by Julia at December 05, 2016 - 9:38 PM

Good: the first collection

Posted by Winnie at December 16, 2016 - 9:42 PM