美文网首页
iOS Run timer 简述

iOS Run timer 简述

作者: 表弟1616 | 来源:发表于2020-04-21 16:18 被阅读0次

一直对iOS的runtimer机制不太理解,或者是理解有偏差,趁着最近不忙,认真的研究了一下,

runtimer 简称运行时,就是系统在运行时候的一些机制,其中最主要的是消息机制,对于c语言,函数的调用在编译的时候会决定调用哪个函数,编译完成之后直接按顺序执行,OC的函数调用属于动态调用,在编译的时候并不能决定真正调用哪个函数,只有在真正的运行时才会根据函数的名字找到对应的函数去调用
obj-c 是基于c语言加入了面向对象特性和消息转发机制的一门动态语言,他不仅需要编译器来编译,还需要runtimer系统来动态的创建类和对象,
那么runTimer可以帮助我们做哪些事情呢,其实目前我使用的地方也不是太多,主要用来做按钮事件的绑定,还有就是替换系统的方法。

run timer 的的简单使用
1.拦截方法
2.交换方法
3.分类中添加添加属性
4.字典转model

一、拦截交换方法

#import "NSArray+Extension.h"
#import <objc/runtime.h>

@implementation NSArray (Extension)

+ (void)load{
    [super load];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{  //方法交换只要一次就好
        Method oldObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndex:));
        Method newObjectAtIndex = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(自己定义的方法:));
        method_exchangeImplementations(oldObjectAtIndex, newObjectAtIndex);
    });

}

二、动态的增加一个属性

#import <UIKit/UIKit.h>

@interface UIControl (Extension)

@property (nonatomic, strong) NSString *tagss;

@end
#import "UIControl+Extension.h"
#import "objc/runtime.h"

static const void * tagsBy = &tagsBy;

@implementation UIControl (Extension)

@dynamic tagss;

- (void)setTagss:(NSString *)tagss{
    
   objc_setAssociatedObject(self, tagsBy, tagss, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

-(NSString *)tagss {
    
    return objc_getAssociatedObject(self, tagsBy);
}

@end

相关文章

  • iOS Run timer 简述

    一直对iOS的runtimer机制不太理解,或者是理解有偏差,趁着最近不忙,认真的研究了一下, runtimer ...

  • ios run timer

    ios run timer 一直对iOS的runtimer机制不太理解,或者是理解有偏差,趁着最近不忙,认真的研究...

  • Timer

    关于Timer Timers work in conjunction with run loops. Run lo...

  • 30-Timer和TimerTask

    Timer和TimerTask Timer就是一个调度器,而TimerTask只是一个实现了run方法的类,而具体...

  • AFNetwork3.0 学习笔记

    常用代码片段 GCD:仅执行一次 常量的声明 或 向RunLoop中添加Timer 注:让需要将Timer从Run...

  • NSTimer 官方文档

    Timer 是在某一时间后触发, 并发送给目标特定消息. Timer 和 run loop 同时工作,并且 tim...

  • iOS Timer

    iOS开发中定时器经常会用到,iOS中常用的定时器有三种,分别是NSTime,CADisplayLink和GCD。...

  • NSTimer+Block

    在ios10以前,使用NSTimer的 会导致self被timer持有,如果timer不主动调用invalidat...

  • Emacs定时任务

    Emacs定时任务 Emacs提供实现定时任务的方式有两种:MidnightMode和run-with-timer...

  • Ionic 真机调试 命令行 iOS/Android

    --iOS--​sudo npm install -g ios-deploy​ionic run ios --de...

网友评论

      本文标题:iOS Run timer 简述

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