runtime 的使用

来源:互联网 发布:50而知天命什么意思 编辑:程序博客网 时间:2024/05/29 04:19

#import <objc/runtime.h>

#import "HaoClass.h"

#import "TwoClass.h"

#import "NSString+Hao.h"

@interface CustomClas : NSObject

{



}

-(void)objectrelease;

@end

@implementation CustomClas


int cfunction (idself,SEL_cmd,NSString * str1,NSString * str2){

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

    

    


    return 99;

    

}


- (void)dealloc

{

    NSLog(@"对象已销毁");

    [superdealloc];

}

-(void)objectrelease{


    HaoClas * cus=[HaoClasnew];

    _Block_object_dispose((__bridgeconst void *)(cus),0);

   // [cus release];

    


    NSLog(@"%lu",(unsignedlong)cus.retainCount);

    


}


@end

int main(int argc,const char * argv[]) {

    @autoreleasepool {

       

   

       //1.  对象的释放:

        CustomClas * cus=[CustomClasnew];

        //class_addMethod(<#Class cls#>, <#SEL name#>, <#IMP imp#>, <#const char *types#>);

        /**

         

            Class cls  ---是为那个类:

         

             SEL name  ---方法的名字

             IMP imp  ----有些类似函数的指针,实现这个函数所做的事情:cfunctionid self,SEL _cmd,参数的类型1,参数的类型2..;

         

           const char * types:----   参数:表示为:--- “i@@”i表示返回值为int类型的, @:参数idself):: SEL(_cmd) @idstr

         

         

         */

        

      

        class_addMethod([CustomClasclass], @selector(ocmethod::), (IMP)cfunction,"i@:@:@");

        

        if ([cusrespondsToSelector:@selector(ocmethod::)]) {

            NSLog(@"ok----");

        }else{

            NSLog(@"失败");

        

        }

        

        int a=(int)[cus ocmethod:@"我的":@"你懂的"];

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

         int b=(int)[cus ocmethod:@"222222oc的方法":@"你懂的"];

         

         

        

       // class_addMethod([CustomClas class], @selector(ocMethod:), (IMP)cfunction, "i@:@");

       // if ([cus respondsToSelector:@selector(ocMethod:)]) {

        //    NSLog(@"是的---响应成功");

       // }else{NSLog(@"失败");}

       // int a=(int)[cus ocMethod:@"我是oc添加的方法"];

       // NSLog(@"A======%d",a);

        

        //_Block_object_dispose((__bridge const void *)(cus),0);

       //runtime可以实现对象的释放:  上面就是

        

        

        //NSLog(@"%lu",(unsigned long)cus.retainCount);

      //[cus release];

            //  NSLog(@"%lu",(unsigned long)cus.retainCount);

     //   id objtest =(__bridge)<#expression#>)objc_copy(obj,sizeof(obj));

        

        

        

        //copyObj;

        

       

        //3。把一个对象更改为其他类的对象   

        

             CustomClass *obj=[CustomClassnew];

        NSLog(@"obj----开始创建的时候------%p",obj);

           //  [obj fun1];

        

        Class aClass=object_setClass(obj, [NSStringclass]);

        //obj的类已被更改为 twoClass类啦

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

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

        

        

         NSLog(@"obj更改过后的时候------%p",obj);//地址还是同一块只是指向变成了 更改的对象类分配的内存地址啦

        //[obj fun1];

       // [[aClass new] fun2];

        

        TwoClass * two=[aClassnew];

        

         NSLog(@"two----开始创建的时候------%p",two);

       [two fun1];

        

   //[obj fun2];

      //  [obj fun1];

        

        TwoClass *twoc=[TwoClassnew];

        Class ac=object_setClass(twoc, [NSStringclass]);

        [twoc add];

       // [twoc objectrelease];

        //isa 指针发生了变化:

        

        //3.2   获取对象的类是什么?

        

        Class getclass=object_getClass(@"NSString");

        

      NSLog(@"getclass----%@",NSStringFromClass(getclass));//twoc在上面的时候object_setclass的时候,变成了nsstring类的对象:

        [[getclass new]add];

        //5---给一个类添加方法:

        //

        // NSString * str=[NSString stringWithFormat:@"hhh"];

        NSString *str=[[NSStringalloc]init];

         NSLog(@"str----开始创建的时候------%p",str);

        class_addMethod([NSStringclass], @selector(ocN::), (IMP)cfunction ,"i@:@:@" );

        

       

        

        if ([strrespondsToSelector:@selector(ocN::)]) {

            NSLog(@"NSString类方法添加成功");

        }else{NSLog(@"失败");}

        

        

        int c=(int)[str ocN:@"HAO":@"NSString新加的方法"];

        NSLog(@"str----结束创建的时候------%p",str);

       // int ccstr=(int)[str ocN:@"woshinsstring",@"NSString的方法"];

        

       printf("nsstring----------%d\n",c);

        

        

        

        

  

        

        

        

        

        

        

    }

    return 0;

}



输出:

2015-10-03 00:19:12.859047 CF-runtime的使用[6279:6331381] ok----

2015-10-03 00:19:12.860416 CF-runtime的使用[6279:6331381] str1----我的,str2----你懂的

2015-10-03 00:19:12.860464 CF-runtime的使用[6279:6331381] a-----99

2015-10-03 00:19:12.860508 CF-runtime的使用[6279:6331381] str1----222222oc的方法,str2----你懂的

2015-10-03 00:19:12.860617 CF-runtime的使用[6279:6331381] obj----开始创建的时候------0x100207730

2015-10-03 00:19:12.860675 CF-runtime的使用[6279:6331381] obj更改过后的时候------0x100207730

2015-10-03 00:19:12.860718 CF-runtime的使用[6279:6331381] two----开始创建的时候------0x100303360

2015-10-03 00:19:12.860751 CF-runtime的使用[6279:6331381] FUN1

2015-10-03 00:19:12.860801 CF-runtime的使用[6279:6331381] nsstring + Hao类别

2015-10-03 00:19:12.860918 CF-runtime的使用[6279:6331381] getclass----__NSCFConstantString

2015-10-03 00:19:12.860964 CF-runtime的使用[6279:6331381] nsstring + Hao类别

2015-10-03 00:19:12.861077 CF-runtime的使用[6279:6331381] str----开始创建的时候------0x7fffb047a9a0

2015-10-03 00:19:12.861179 CF-runtime的使用[6279:6331381] NSString类方法添加成功

2015-10-03 00:19:12.861245 CF-runtime的使用[6279:6331381] str1----HAO,str2----NSString新加的方法

2015-10-03 00:19:12.861291 CF-runtime的使用[6279:6331381] str----结束创建的时候------0x7fffb047a9a0

nsstring----------99

Program ended with exit code: 0










原创粉丝点击