美文网首页2017 iOS跳槽宝典
面试篇(六):RunTime 熟悉吗?

面试篇(六):RunTime 熟悉吗?

作者: L柠_檬 | 来源:发表于2016-12-30 17:09 被阅读44次

RunTime 是做什么的?

RunTime简称运行时。OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消息机制

你都在哪些方面用过RunTime,可以举例说明吗?

  • 拦截系统自带的方法调用
1.监控移动端上是否安装了某个App。
#import < objc/runtime.h >

Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");

NSObject *workspace = [LSApplicationWorkspace_class 
performSelector:@selector(defaultWorkspace)];

BOOL installed = [workspace performSelector:@selector(applicationIsInstalled:) 
withObject:@"com.xxxxx.cx"];

NSLog(@"%d",installed);
 
  • 交换方法
1.UIImage 加载图片,图片不存在的时候提示。

#import "UIImage+Category.h"
#import <objc/runtime.h>

@implementation UIImage (Category)

+ (void)load{
    
    // 获取imageWithName方法地址
    Method imageWithName = class_getClassMethod(self, @selector(imageWithName:));
    
    // 获取imageWithName方法地址
    Method imageName = class_getClassMethod(self, @selector(imageNamed:));
    
    // 交换方法地址,相当于交换实现方式
    method_exchangeImplementations(imageWithName, imageName);
    
}

+ (instancetype)imageWithName:(NSString *)name
{
    
    // 这里调用imageWithName,相当于调用imageName
    UIImage *image = [self imageWithName:name];
    
    if (image == nil) {
        NSLog(@"%@ 没有找到图片",name);
    }
    
    return image;
}

Log.png

最近比较忙,待更~

相关文章

  • 面试篇(六):RunTime 熟悉吗?

    RunTime 是做什么的? RunTime简称运行时。OC就是运行时机制,也就是在运行时候的一些机制,其中最主要...

  • 01·iOS 面试题·项目中用过 Runtime 吗?

    01·iOS 面试题·项目中用过 Runtime 吗? 01·iOS 面试题·项目中用过 Runtime 吗?

  • OC的Runtime能干什么?

    参考文章:iOS 面试题·项目中用过 Runtime 吗?iOS 模块分解—「Runtime面试、工作」看我就ok...

  • iOS-Runtime

    一.什么是runtime? 话说每次面试都问runtime,你不会runtime能拿到像我66k的工资吗?所以关于...

  • ios runtime 详解

    前言 在开始之前建议先阅读iOS runtime的基础理解篇:iOS内功篇:runtime 有筒子在面试的时候,遇...

  • iOS runtime实战应用:成员变量和属性

    前言 在开始之前建议先阅读iOS runtime的基础理解篇:iOS内功篇:runtime 有筒子在面试的时候,遇...

  • iOS runtime实战应用:关联对象

    前言 在开始之前建议先阅读iOS runtime的基础理解篇:iOS内功篇:runtime 有筒子在面试的时候,遇...

  • Runtime应用--给分类添加属性

    iOS面试经常会被问到:你有用过Runtime吗? 使用Runtime做过什么? 开发中我们会经常使用分类,知道分...

  • Runtime

    这一次的面试过程中,总有面试官问我:你了解 runtime 是什么?你了解 runtime 吗?你在项目中用过 r...

  • OC底层探索-Runtime简介

    说起RunTime作为iOS开发者就再熟悉不过,面试基本上一大部分原理性的问题都是出自这里。 runtime官方文...

网友评论

    本文标题:面试篇(六):RunTime 熟悉吗?

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