zoukankan      html  css  js  c++  java
  • ios高级开发,runtime(一)

    创建公共类:

    @interface CustomClass : NSObject

    - (void) fun1;

    @end

    @implementation CustomClass

    -(void)fun1

    {

        NSLog(@"fun1");

    }

    @end

    @interface TestClass : NSObject

    @end

    @implementation TestClass

    @end

    1、对象拷贝:id object_copy(id obj, size_t size)

    CustomClass * obj = [CustomClass new];

            NSLog(@"%p",&obj);

            

            id objTest = object_copy(obj, sizeof(obj));

            NSLog(@"%p",&objTest);

            

            [objTest fun1];

     

    打印结果:

    2014-10-20 09:51:48.973 runtimetest[2573:303] 0x7fff5fbff818

    2014-10-20 09:51:48.983 runtimetest[2573:303] 0x7fff5fbff810

    2014-10-20 09:51:48.984 runtimetest[2573:303] fun1

    说明:

    object_copy 函数实现了对象的拷贝。

    2、对象释放 id object_dispose(id obj)

     

    object_dispose(objTest);

            [objTest release];

    runtimetest(2791,0x7fff791b2310) malloc: *** error for object 0x100202950: pointer being freed was not allocated

    3、更改对象的类/获取对象的类Class object_setClass(id obj, Class cls)  / Class object_getClass(id obj)

            CustomClass * obj = [CustomClass new];

            [obj fun1];

            

            Class aClass = object_setClass(obj, [CustomClassOther class]);

            NSLog(@"aClass:%@",NSStringFromClass(aClass));

            

            NSLog(@"obj class:%@",NSStringFromClass([obj class]));

            [obj fun2];

    运行结果:

    2014-10-20 10:09:24.619 runtimetest[3536:303] aClass:CustomClass

    2014-10-20 10:09:24.619 runtimetest[3536:303] obj class:CustomClassOther

    2014-10-20 10:09:24.620 runtimetest[3536:303] fun2

    CustomClass * obj = [CustomClass new];

            Class objClass = object_getClass(obj);

            NSLog(@"%@",NSStringFromClass(objClass));

    运行结果:

    2014-10-20 10:12:34.047 runtimetest[3726:303] CustomClass

    4、获取对象的类名  constchar *object_getClassName(id obj)

    CustomClass * obj = [CustomClass new];

            NSString * className =[NSString stringWithCString:object_getClassName(obj) encoding:NSUTF8StringEncoding];

            NSLog(@"%@",className);

    运行结果:

    2014-10-20 10:22:54.514 runtimetest[4160:303] CustomClass

    5、给一个类添加方法  

    BOOL class_addMethod(Class cls,SEL name,IMP imp, 

     

    const char *types)

     

     

     

     

    /**

      * 一个参数

      *

      */

     

    int cfunction(id self, SEL _cmd, NSString *str) {

        NSLog(@"%@", str);

        return10;//随便返回个值

    }

     

    - (void) oneParam {

        

        TestClass *instance = [[TestClassalloc]init];

        //    方法添加

        class_addMethod([TestClassclass],@selector(ocMethod:), (IMP)cfunction,"i@:@");

        

        if ([instance respondsToSelector:@selector(ocMethod:)]) {

            NSLog(@"Yes, instance respondsToSelector:@selector(ocMethod:)");

        } else

        {

            NSLog(@"Sorry");

        }

        

        int a = (int)[instanceocMethod:@"我是一个OCmethodC函数实现"];

        NSLog(@"a:%d", a);

     

    }

    2014-10-20 10:47:25.738 runtimetest[5333:303] Yes, instance respondsToSelector:@selector(ocMethod:)

    2014-10-20 10:47:25.748 runtimetest[5333:303] 我是一个OCmethodC函数实现

    2014-10-20 10:47:25.749 runtimetest[5333:303] a:10

     

     

     /**

      * 两个参数

      *

      */

     

    int cfunctionA(id self, SEL _cmd, NSString *str, NSString *str1) {

        NSLog(@"%@-%@", str, str1);

        return20;//随便返回个值

    }

     

    - (void) twoParam {

        

        TestClass *instance = [[TestClassalloc]init];

        

        class_addMethod([TestClassclass],@selector(ocMethodA::), (IMP)cfunctionA,"i@:@@");

        

        if ([instance respondsToSelector:@selector(ocMethodA::)]) {

            NSLog(@"Yes, instance respondsToSelector:@selector(ocMethodA::)");

        } else

        {

            NSLog(@"Sorry");

        }

        

        int a = (int)[instanceocMethodA:@"我是一个OC的method,C函数实现" :@"-----我是第二个参数"];

        NSLog(@"a:%d", a);

    }

  • 相关阅读:
    Spark架构分析
    mr运行出错,解决办法
    hbase调优
    虚拟机长时间不关造成的问题
    crontab 使用
    虚拟机克隆网络问题的解决
    ligerui.grid.extend.rowSpan
    64位下安装Scrapy 报错 "could not find openssl.exe" 的解决方法。
    EventBus 事件总线之我的理解
    MongoDB 系列(二) C# 内嵌元素操作 聚合使用
  • 原文地址:https://www.cnblogs.com/q403154749/p/4036738.html
Copyright © 2011-2022 走看看