Runtime的使用

2016-10-11  本文已影响0人  figure_ai

一、runtime简介


二、runtime的作用

1.发送消息

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //消息机制的简单使用
    
    //创建peerson对象
    Person *p = [[Person alloc] init];
     
    //调用对象方法
    //    [p eat];
    
    //SEL:方法编号,根据方法编号就可以 找到对应方法实现
//    [p performSelector:@selector(eat)];
    
    //使用运行时,发送消息,谁做事情就拿谁
    //让p发送消息
    objc_msgSend(p,@selector(eat));
    objc_msgSend(p, @selector(run:),90);
    
    //类名调用类方法,本质上是将类名转换成类对象
//    [Person eat];
    Class personClass = [Person class];
    [personClass  performSelector:@selector(eat)];
    

}
Snip20151013_2.png

2.交换方法

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 需求:给imageNamed方法提供功能,每次加载图片就判断下图片是否加载成功。
    // 步骤一:先搞个分类,定义一个能加载图片并且能打印的方法+ (instancetype)imageWithName:(NSString *)name;
    // 步骤二:交换imageNamed和imageWithName的实现,就能调用imageWithName,间接调用imageWithName的实现。
    UIImage *image = [UIImage imageNamed:@"123"];

}

@end


@implementation UIImage (Image)
// 加载分类到内存的时候调用
+ (void)load
{
    // 交换方法

    // 获取imageWithName方法地址
    Method imageWithName = class_getClassMethod(self, @selector(imageWithName:));

    // 获取imageWithName方法地址
    Method imageName = class_getClassMethod(self, @selector(imageNamed:));

    // 交换方法地址,相当于交换实现方式
    method_exchangeImplementations(imageWithName, imageName);


}

// 不能在分类中重写系统方法imageNamed,因为会把系统的功能给覆盖掉,而且分类中不能调用super.

// 既能加载图片又能打印
+ (instancetype)imageWithName:(NSString *)name
{

    // 这里调用imageWithName,相当于调用imageName
    UIImage *image = [self imageWithName:name];

    if (image == nil) {
        NSLog(@"加载空的图片");
    }

    return image;
}


@end
Snip20151013_3.png

3.动态添加方法

- (void)viewDidLoad {
    [super viewDidLoad];
    

    dttjff *p = [[dttjff alloc] init];
    
    //默认没有实现eat方法,可以通过performselector调用,但是会报错
    //动态添加方法就不会报错
    [p performSelector:@selector(eat)];
}

 #import "dttjff.h"
 #import <objc/message.h>

@implementation dttjff

void eatImp()
{
    NSLog(@"调用了eat方法");
}
/**
 *什么时候调用:当调用了一个没有实现的方法就会调用resolveInstanceMethod
 *作用:获取到哪些方法没有实现,从而实现动态添加方法
 *sel:没有实现的方法
 */
+(BOOL)resolveInstanceMethod:(SEL)sel
{
    
    if (sel == @selector(eat)) {
        
        /*第一个参数cls:给哪个类添加方法
         第二个参数SEL:添加方法的方法编号是什么
         第三个参数IMP:方法实现,函数入口,函数名
         第四个参数types:方法类型
         */
        class_addMethod([self class], sel, (IMP)eatImp, "v");
        return YES;
    
    }
    
    //调用系统方法
    return [super resolveInstanceMethod:sel];
    
}

@end

4.给分类添加属性




@implementation NSObject (objc)

- (void)setName:(NSString *)name
{
    //第一个参数:给哪个对象添加属性
    //第二个参数:关联的key,通过这个key获取
    //第三个参数:关联的value
    //第四个参数:   关联的策略
    objc_setAssociatedObject(self, @"name", name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)name
{
    //根据关联的key,获取关联的值
    return objc_getAssociatedObject(self, @"name");
}
@end


#import "ViewController.h"
#import "NSObject+objc.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //给NSObject动态添加属性name
    NSObject *objc = [[NSObject alloc]init];
    objc.name = @"666";
    NSLog(@"%@",objc.name);
}

@end

注意:默认在分类中声明属性不会生成set和get方法。

上一篇 下一篇

猜你喜欢

热点阅读