ios动态创建类Class4887王中王鉄算盘奖结果

来源:http://www.smjxgs.com 作者:操作系统 人气:121 发布时间:2019-08-11
摘要:ios动态创建类Class,ios动态class 【Objective-C Runtime动态加载】---动态创造类Class 动态创建类Class,动态增多Class成员变量与成员函数,动态变量赋值与取值,动态函数调用等艺术 a.使用objc_a

ios动态创建类Class,ios动态class

【Objective-C Runtime动态加载】---动态创造类Class

动态创建类Class,动态增多Class成员变量与成员函数,动态变量赋值与取值,动态函数调用等艺术

a.使用objc_allocateClassPair创制叁个类Class
    const char * className = "Calculator";
    Class kclass = objc_getClass(className);
    if (!kclass) 
    {
       Class superClass = [NSObject class]; 
       kclass = objc_allocateClassPair(superClass, className, 0);
    }

b.使用class_addIvar增加贰个成员变量
    NSUInteger size;
    NSUInteger alignment;
    NSGetSizeAndAlignment("*", &size, &alignment);
    class_addIvar(kclass, "expression", size, alignment, "*");

    注:
    1.type定义参考:
    2."*"星号代表字符(),iOS字符为4位,并应用4位对齐kclass

c.使用class_addMethod加多成员方法
    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");
    } 

    注:
    1.type概念参考:
    2."v@:@",解释v-重返值void类型,@-self指针id类型,:-SEL指针SEL类型,@-函数第2个参数为id类型
    3."@@:",解释@-返回值id类型,@-self指针id类型,:-SEL指针SEL类型,
d.注册到运营时情状
    objc_registerClassPair(kclass);
e.实例化类
    id instance = [[kclass alloc] init];
f.给变量赋值
   object_setInstanceVariable(instance, "expression", "1 1"); 
g.获得变量值
    void * value = NULL;
    object_getInstanceVariable(instance, "expression", &value); 
h.调用函数
    [instance performSelector:@selector(getExpressionFormula)];

【Objective-C Runtime动态加载】---动态创造类Class

动态创制类Class,动态加多Class成员变量与成员函数,动态变量赋值与取值,动态函数调用等方法

a.使用objc_allocateClassPair创立叁个类Class
    const char * className = "Calculator";
    Class kclass = objc_getClass(className);
    if (!kclass) 
    {
       Class superClass = [NSObject class]; 
       kclass = objc_allocateClassPair(superClass, className, 0);
    }

b.使用class_addIvar加多一个成员变量
    NSUInteger size;
    NSUInteger alignment;
    NSGetSizeAndAlignment("*", &size, &alignment);
    class_addIvar(kclass, "expression", size, alignment, "*");

    注:
    1.type定义参谋:
    2."*"星号代表字符(),iOS字符为4位,并应用4位对齐kclass

c.使用class_addMethod增添成员方法
    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");
    } 

    注:
    1.type概念参考:
    2."v@:@",解释v-再次回到值void类型,@-self指针id类型,:-SEL指针SEL类型,@-函数第二个参数为id类型
    3."@@:",解释@-返回值id类型,@-self指针id类型,:-SEL指针SEL类型,
d.注册到运维时景况
    objc_registerClassPair(kclass);
e.实例化类
    id instance = [[kclass alloc] init];
f.给变量赋值
   object_setInstanceVariable(instance, "expression", "1 1"); 
g.得到变量值
    void * value = NULL;
    object_getInstanceVariable(instance, "expression", &value); 
h.调用函数
    [instance performSelector:@selector(getExpressionFormula)];

【Objective-C Runtime动态加载】---动态创造类Class

动态创造类Class,动态增加Class成员变量与成员函数,动态变量赋值与取值,动态函数调用等措施
a.使用objc_allocateClassPair成立二个类Class
    const char * className = "Calculator";
    Class kclass = objc_getClass(className);
    if (!kclass) 
    {
       Class superClass = [NSObject class]; 
       kclass = objc_allocateClassPair(superClass, className, 0);
    }
b.使用class_addIvar加多三个分子变量
    NSUInteger size;
    NSUInteger alignment;
    NSGetSizeAndAlignment("*", &size, &alignment);
    class_addIvar(kclass, "expression", size, alignment, "*");
    注:
    1.type概念仿照效法:
    2."*"星号代表字符(),iOS字符为4位,并使用4位对齐kclass
c.使用class_addMethod增加成员方法
    class_addMethod(kclass, @selector(setExpressionFormula:), (IMP)setExpressionFormula, "[email protected]:@");
    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");
    } 
    注:
    1.type定义参考:
    2."[email protected]:@",解释v-重返值void类型,@-self指针id类型,:-SEL指针SEL类型,@-函数第二个参数为id类型
    3."@@:",解释@-返回值id类型,@-self指针id类型,:-SEL指针SEL类型,
d.注册到运维时遭受
    objc_registerClassPair(kclass);
e.实例化类
    id instance = [[kclass alloc] init];
f.给变量赋值
   object_setInstanceVariable(instance, "expression", "1 1"); 
g.得到变量值
    void * value = NULL;
    object_getInstanceVariable(instance, "expression", &value); 
h.调用函数
    [instance performSelector:@selector(getExpressionFormula)];

【Objective-C Runtime动态加载】---动态创立类Class 动态成立类Class,动态增加Class成员变量与成员函数,动态变量赋...

本文由4887王中王鉄算盘奖结果发布于操作系统,转载请注明出处:ios动态创建类Class4887王中王鉄算盘奖结果

关键词:

最火资讯