runtime的一些简单用法

先来简单介绍一下runtime:
我们都知道Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理。这种动态语言的优势在于:我们写代码时更具灵活性,如我们可以把消息转发给我们想要的对象,或者随意交换一个方法的实现等。
对于Objective-C来说,这个运行时系统就像一个操作系统一样:它让所有的工作可以正常的运行。这个运行时系统即Objc Runtime。Objc Runtime其实是一个Runtime库,它基本上是用C和汇编写的,这个库使得C语言有了面向对象的能力。
我们编写OC代码时候,编译过程中首先会将OC语言转换成runtime(其实就是C语言) - - >然后再变成汇编 - - >最后成机器码。大概理解一下这个原理即可,下面上代码理解一下runtime的一些简单使用。

  • 通过runtime获取一个对象的所有方法:
    #import "objc/runtime.h" //得加入头文件

      unsigned int methCout_f =0;
      Method* methList_f = class_copyMethodList([test class],&methCout_f);
      for(int i=0;i<methCout_f;i++)
      {
      Method temp_f = methList_f[i];
      IMP imp_f = method_getImplementation(temp_f);
      SEL name_f = method_getName(temp_f);
      const char* name_s =sel_getName(method_getName(temp_f));
      int arguments = method_getNumberOfArguments(temp_f);
      const char* encoding =method_getTypeEncoding(temp_f);
      NSLog(@"方法名:%@,参数个数:%d,编码方式:%@",[NSString stringWithUTF8String:name_s],
            arguments,
            [NSString stringWithUTF8String:encoding]);
       }
     free(methList_f);
    
  • 通过runtime获取一个对象的所有属性:
    u_int count;
    objc_property_t *properties = class_copyPropertyList([object class], &count);
    NSMutableArray propertiesArray = [NSMutableArray array];
    for (int i = 0; i < count ; i++)
    {
    const char
    propertyName = property_getName(properties[i]);
    [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
    }

    free(properties);
    return propertiesArray;
    
  • 关联对象改变获取值的方式
    //设置一个全局静态指针,
    static char * const associativekey = "associativekey";

     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"what do you want to do?" delegate:self cancelButtonTitle:@"YES" otherButtonTitles:@"cancel", nil];
     [alert show];
    
    __weak typeof(self) weakSelf = self;
    void(^block)(NSInteger) = ^(NSInteger index){
    [weakSelf youWantDoAnyThing];
      NSLog(@"======%d",index);
    };
    objc_setAssociatedObject(alert, associativekey, block, OBJC_ASSOCIATION_COPY);  
     }
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    void(^block1)(NSInteger)  = objc_getAssociatedObject(alertView, associativekey);
    
     block1(buttonIndex);
    
    }
    

参考:http://southpeak.github.io/blog/2014/10/25/objective-c-runtime-yun-xing-shi-zhi-lei-yu-dui-xiang/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,858评论 0 9
  • 本篇文章在《iOS开发之Runtime常用示例总结》基础上修改,地址是「:」http://www.cocoachi...
    小__小阅读 1,861评论 1 3
  • Objective-C语言是一门动态语言,他将很多静态语言在编译和链接时期做的事情放到了运行时来处理。这种动态语言...
    tigger丨阅读 1,471评论 0 8
  • 原文出处:南峰子的技术博客 Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了...
    _烩面_阅读 1,321评论 1 5
  • 我们常常会听说 Objective-C 是一门动态语言,那么这个「动态」表现在哪呢?我想最主要的表现就是 Obje...
    Ethan_Struggle阅读 2,243评论 0 7