美文网首页
iOS-Runtime在开发中的使用及相关面试题

iOS-Runtime在开发中的使用及相关面试题

作者: 起个名字好难O0 | 来源:发表于2017-03-06 11:34 被阅读75次

    OC语言中最为强大的莫过于OC的运行时机制-Runtime,但因其比较接近底层,一旦使用Runtime出现bug,将很难调试,所以Runtime在开发中能不用就不用.下面我将介绍一些Runtime在开发中的使用,已经面试可能遇见的面试题.

    1.OC语法和Runtime语法的区别

    OC语法和Runtime语法的区别,换而言之就是OC中我们写的语句,最终被转换成Runtime中什么样语句.由于Xcode6之后,苹果不建议使用Runtime,也就是现在在编译的时候,runtime的函数不会提示,需要去配置一下:
    // 配置步骤: build Seting -> 搜索msg -> 设置成NO
    创建一个控制台程序,在自动释放池中写如下代码:

    NSObject *objc = [NSObject alloc];        
     objc = [objc init];  
    

    然后切换到终端命令行,执行以下步骤:
    cd 切换到你想生成的那个根文件的上一级目录
    clang -rewrite-objc main.m // clang -rewrite-objc 目标文件 会在该目录文件下生成一个.cpp文件,打开之后搜索@autoreleasepool(这也就是当时为什么创建控制器程序的原因,好查找转换后的代码在哪儿),就会找到转换后的代码:

     NSObject *objc = ((NSObject *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("NSObject"), sel_registerName("alloc"));   
    objc = ((NSObject *(*)(id, SEL))(void *)objc_msgSend)((id)objc, sel_registerName("init"));   
    

    上面的代码比较原生态,我们要是直接写runtime的代码如下所示,就能达到创建一个NSObject对象的目的:
    // objc_msgSend: 两个参数 1. 谁发送这个消息 2. 发送给谁

     NSObject *objc =  objc_msgSend([NSObject class], @selector(alloc));    
     objc = objc_msgSend(objc, @selector(init)); 
    

    2.消息机制,调用私有方法

    面试题: runtime是什么?或者是同类的

    答: 其实runtime就是运行时机制,可以通过命令行clang -rewrite-objc 对应的目标文件,就能将对应的OC的代码转成对应的运行时的代码

    若是面试官问runtime中是怎么找到对应的方法的,该怎么回答?

    答: 首先确定问的是对象方法还是类方法,对象方法保存到类中,类方法保存到元类(meta class),每一个类都有方法列表methodList,每一个方法在方法列表中都有对应的方法编号.
    (1)根据对象的isa去对应的类查找方法,isa: 判断去哪个类找对应的方法,指向方法调用的类
    (2)根据传入的方法编号,才能在方法列表中找到对应得方法Method(方法名).
    (3)根据方法名(函数入口)找到函数实现
    知识扩充: 其实每个方法最终转换成函数的形式,存放在方法区,而每一个函数的函数名都是函数的入口

    访问类中私有方法的代码如下:

    在对应类中的@implementation实现私有方法:

     #import "Person.h"   
    @implementation Person  
     - (void)eat {    
          NSLog(@"吃吃吃"); 
    } 
      - (void)run: (int)num {  
            NSLog(@"跑了%d米", num);
     } 
    @end 
    

    在ViewController.m中的代码如下:

     #import "ViewController.h"
     #import "Person.h" 
    #import <objc/message.h> 
     /*      runtime: 千万不要随便使用,不得已才使用     消息机制:  1. 装逼   2. 调用已知私有的方法  */  
    @interface ViewController ()  
    
    @end   
    @implementation ViewController   
    - (void)viewDidLoad {         
              Person *p = objc_msgSend([Person class], @selector(alloc)); 
              p = objc_msgSend(p, @selector(init));      
     //    objc_msgSend(p, @selector(eat)); 
             objc_msgSend(p, @selector(run:),20);
     } 
    @end 
    

    注意: 一定要导入runtime的头文件 : #include <objc/runtime.h> 或者 #import <objc/message.h>

    3.runtime方法交换
    需求1: 我现在有一个项目,已经开发了两年,之前都是用UIImage中的imageNamed去加载图片,但是组长现在想imageNamed,给我提示是否加载成功.

    思想1:在分类实现该方法.(但这种方法会把系统的方法覆盖,一般不采用)
    思想2: 自定义一个Image类,为什么不采用这种方法(这里你就要明白什么时候需要自定义,系统功能不完善,就定义这样一个类,去扩展这个类)
    前两种方法都有一定的局限性,若是项目开发很久了,就需要更改好多东西,利用runtime交换方法实现的作用,可以简单的实现这个需求
    这个时候不得不用runtime去交换方法
    分类中代码如下
    UIImage+image.h

    #import <UIKit/UIKit.h>   
    @interface UIImage (image)  
     + (UIImage *)BO_imageNamed:(NSString *)name;
     @end 
    

    分类中代码如下UIImage+image.m

     #import "UIImage+image.h" 
    #import <objc/message.h> 
    @implementation UIImage (image)  
     //如果当前类中东西仅且只需加载一次,一般放在load中.当然也可以放在initialize中,需要进行判断调用该类的是的类的类型  
     // 加载类的时候会调用,仅且调用一次 
    + (void)load {         
     // 首先要拿到要交换的两个方法     
         Method method1 = class_getClassMethod([UIImage class],@selector(BO_imageNamed:));     
         Method method2 = class_getClassMethod([UIImage class],@selector(imageNamed:));
         method_exchangeImplementations(method1, method2); 
    }
     // 加载当前类或者子类时候.会调用.可能会调用不止一次 
    + (void)initialize  { 
         
     }  
    // 在系统方法的之前加前缀名的作用,防止覆盖系统方法,有开发经验的人默认的 + (UIImage *)BO_imageNamed:(NSString *)name{
          // 当运行到这儿时,这里已经是imageNamed中的内容,此时再调用BO_imageNamed相当于原来imageNamed中的内容    
     UIImage *image = [self BO_imageNamed:name];          
          if (image == nil) {         
               NSLog(@"照片不存在");    
          }           
         return image; 
    }
     @end 
    

    调用的代码如下:

    #import "ViewController.h" 
    //#import "BOImage.h" 
    #import "UIImage+image.h"
     /*      需求: 不得不用runtime去交换方法      需求: 想要在调用imageNamed,就给我提示,是否加载成功     需求: 让UIImage调用imageNamed有这个功能        需求: 比如我有一个项目,已经开发两年,之前都是用UIImage去加载图片.组长现在想调用imageNamed,就给我提示,是否加载成功             注意: 在分类中一定不要重写系统方法,否则就把系统方法干掉了      思想: 什么时候需要自定义,系统功能不完善,就定义一个这样的类,去扩展这个类   //    前两种方法都有一定的局限性,若是项目开发很久了,则需要更改好多东西,利用runtime交换方法实现的作用.可以简单的实现这个需求    */ 
    @interface ViewController () 
     @end   
    @implementation ViewController  
     -(void)viewDidLoad {    
          [super viewDidLoad];       
    //    [BOImage imageNamed:@"123"];    
          [UIImage BO_imageNamed:@"123"];      
     }   
     - (void)didReceiveMemoryWarning {    
          [super didReceiveMemoryWarning];      
        // Dispose of any resources that can be recreated. 
    }   
    @end 
    
    4: 动态添加方法

    应用场景:

    为什么动态添加方法?

    OC中是懒加载,有的方法可能很久不会调用,例如: 电商,视频,社交,收费项目,会员机制,只有会员才拥有这些动能

    下面是道美团面试题:

    面试官问: 有没有使用过performSelector----->其实这里面试官想问的是你有没有动态的添加过方法
    这里就应该这样答: 使用过--->什么时候使用----动态添加方法的时候使用--->为什么动态添加方法---又回到到上面说的什么时候动态添加方法.
    代码如下:

    #import "Person.h" 
    #import <objc/message.h>  
    @implementation Person   
    void eat(id self, SEL _cmd) {    
          NSLog(@"我终于成功了"); 
    } 
     // 动态添加实例方法 
     //resolveInstanceMethod 什么时候调用?只要调用没有实现的方法,就会产生方法去解决,这个方法有什么作用: 去解决没有实现方法,动态添加方法 
    + (BOOL)resolveInstanceMethod:(SEL)sel {
          if (sel == @selector(eat)) { 
              /**           给一个类添加方法           
           @param self 给谁添加方法         
           @param sel 添加那个方法          
           @param IMP 方法实现,函数入口          
           @return 方法类型          */         
                class_addMethod(self, sel, (IMP)eat, "v@:");     
            }      
            return [super resolveInstanceMethod:sel]; 
    }  
      // 动态添加类方法 
     //+ (BOOL)resolveClassMethod:(SEL)sel { 
    //     
    //} 
    @end 
     // 下面是各个字母代表的参数 
    //c  A char 
    //i  An int 
    //s  A short 
    //l  A long 
    //l  is treated as a 32-bit quantity on 64-bit programs. 
    //q  A long long 
    //C  An unsigned char 
    //I  An unsigned int
    //S  An unsigned short 
    //L  An unsigned long
    //Q  An unsigned long long 
    //f  A float 
    //d  A double  
    //B  A C++ bool or a C99 _Bool 
    //v  A void 
    //*  A character string (char *) 
    //@  An object (whether statically typed or typed id) 
    //#  A class object (Class) 
    //:  A method selector (SEL) 
    //[array type]  An array 
    //{name=type...}  A structure 
    //  (name=type...) A union 
    // bnum A bit field of num bits 
    //^type  A pointer to type 
    // ?  An unknown type (among other things, this code is used for function pointers) 
    

    控制器中方法如下:

    #import "ViewController.h" 
    #import "Person.h"  
    /*      动态添加方法:      
           为什么动态添加方法? OC都是懒加载,有些方法可能很久不会调用.例如: 电商,视频,社交,收费项目,会员机制,只有会员才拥有这些动能     
    美团面试题 : 有没有使用过performSelector,使用,什么时候使用,动态添加方法的时候使用,为什么动态添加方法?  OC都是懒加载,有些方法可能很久不会调用.例如: 电商,视频,社交,收费项目,会员机制,只有会员才拥有这些动能    */ 
     @interface ViewController ()  
    
    @end   
    @implementation ViewController   
    - (void)viewDidLoad {
         [super viewDidLoad];
         Person *p = [[Person alloc] init];     
         [p performSelector:@selector(eat)]; 
    }    
    - (void)didReceiveMemoryWarning {
         [super didReceiveMemoryWarning];     
     // Dispose of any resources that can be recreated.
     }   
    @end 
    
    5.动态添加属性

    理论上在分类中@property的作用:
    仅仅是生成get,set方法的声明,并不会生成get,set方法实现,并不会生成下划线属性
    动态添加方法实现思路: 在分类中用@property添加set,get方法之后,其实添加属性就是要把一个变量跟一个类联系起来.也就是在set和get方法中处理,代码如下所示给NSObject添加一个name属性:
    分类中代码 .h:

      #import <Foundation/Foundation.h>  
     @interface NSObject (Property)   
    // @property 在分类中作用 : 仅仅是生成get,set方法声明.并不会生成get,set方法实现,并不会生成下划线成员属性 
    @property NSString *name; 
    
    @end
    

    .m

    #import "NSObject+Property.h"
    #import <objc/message.h>   
    @implementation NSObject (Property)   
    - (void)setName:(NSString *)name {
          objc_setAssociatedObject(self, "name", name, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
    }  
     - (NSString *)name {
          return objc_getAssociatedObject(self, "name");
     }
    @end
    

    控制器中代码:

    #import "ViewController.h" 
    #import "NSObject+Property.h"
     /*      开发的时候,是自己最熟悉什么用什么,而不是什么逼格高用什么,rumtime比较接近底层的语言,不好调试,尽量少用        需求: 给NSObject添加一个name属性,动态添加属性 ->runtime        属性的本质: 让一个属性和对象产生关联  */ 
    @interface ViewController ()  
    
    @end
    @implementation ViewController   
    - (void)viewDidLoad {
         [super viewDidLoad];   
         NSObject *objc = [[NSObject alloc] init];           
         objc.name = @"123";           
         NSLog(@"%@", objc.name); 
    }   
    @end 
    
    6:利用运行时,自己添加属性

    如果一个字典中,有很多的key,如果你在字典转模型的时候,逐个的写下属性,将会非常蛋疼,其实可以给字典添加一个分类,利用遍历字典中key,value,再利用字符串的拼接即可实现. NSDictionary+propertyCode.h分类中代码如下:

    #import <Foundation/Foundation.h>   
    @interface NSDictionary (propertyCode)   
    - (void)createProperty; 
    @end
    

    NSDictionary+propertyCode.m:

    #import "NSDictionary+propertyCode.h"   
    @implementation NSDictionary (propertyCode)   
    - (void)createProperty { 
              [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull value, BOOL * _Nonnull stop) {        
     // 当然这里还是可以自己添加其他类型,就不一一列举         
                    if ([value isKindOfClasswww.baiyuewang.net:[NSString class]]) { 
                             NSLog(@"%@", [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@", key]);         
                      }else if ([value isKindOfClass:[NSArray class]]) {
                             NSLog(@"%@", [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@", key]);
                       }else if ([value isKindOfClass:[NSNumber class]]) { 
                             NSLog(@"%@", [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger key"]);
                       }         
               }]; 
    } 
    @end 
    

    控制器中代码:

    #import "ViewController.h"  
    #import "NSDictionary+propertyCode.h" 
    @interface ViewController ()   
    @property (nonatomic, strong) NSArray *array; 
    @end   
    @implementation ViewController
     - (NSArray *)array {    
              if (_array == nil) { 
                    _array = [NSArray array];
                }      return _array; } 
    - (void)viewDidLoad {
             [super viewDidLoad];          
     // 这里需要拿到一个plist文件或者一个设置一个字典      
             self.array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars.plist" ofType:nil]];                
             for (NSInteger i = 0; i < self.array.count; i++) {
                       NSDictionary *dict = self.array[i]; 
                       [dict createProperty];
             }  
    //    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"" ofType:nil]]; 
    }   
    @end
    

    引入“http://wenku.baidu.com/link?url=o8KdOaol2glqd7EmG1A2rRPuVmb3RBwHzljJFuqaPtl7DsN-WyZVHtyjVckVdEctrC0XbL5h-LlmZkEvoSXurqn2fUi4tGmgpvFebhQml4E0MT6pbwgIlnSAu-yUY4tT

    相关文章

      网友评论

          本文标题:iOS-Runtime在开发中的使用及相关面试题

          本文链接:https://www.haomeiwen.com/subject/ouiygttx.html