运行时小探

作者: 木头与琉璃 | 来源:发表于2016-08-06 15:29 被阅读42次

一个类(Class)维护一张调度表(dispatch table)用于解析运行时发送的消息;调度表中的每个实体(entry)都是一个方法(Method),其中key值是一个唯一的名字——选择器(SEL),它对应到一个实现(IMP)——实际上就是指向标准C函数的指针。

给分类中添加属性

关键点:关联对象
.h文件

#import "MYFObject.h"
@interface MYFObject (Addation)
@property (copy, nonatomic) NSString *name;
@end

.m文件

#import "MYFObject+Addation.h"
#import <objc/runtime.h>
@implementation MYFObject (Addation)
- (NSString *)name{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setName:(NSString *)name{
    objc_setAssociatedObject(self, @selector(name), name, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
@end

在控制器中的使用

 MYFObject *object = [[MYFObject alloc]init];
 object.name = @"慕云飞";
 NSLog(@"%@",object.name);

打印结果

2016-08-06 15:26:15.801 SwizzleDemo[1990:580405] ****慕云飞

替换系统的发表方法

  • 重写viewWillAppear

#import "UIViewController+Addation.h"
#import <objc/runtime.h>
@implementation UIViewController (Addation)
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{ 
        Class class = [self class];
        SEL originalSelector = @selector(viewWillAppear:);
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        
        SEL swizzledSelector = @selector(MYFViewWillAppear:);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        BOOL didAddMethod =
        class_addMethod(class,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));
        
        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        } 
    });
}
//pragma mark - Method Swizzling
- (void)MYFViewWillAppear:(BOOL)animated {
    [self MYFViewWillAppear:animated];
    NSLog(@"viewWillAppear: %@", self); 
}

打印结果

2016-08-07 22:17:37.961 SwizzleDemo[2932:670540] viewWillAppear: <ViewController: 0x7f974141a790>**

  • 设置label的backgroundColor

我将 .h和.m放在一块

#import <UIKit/UIKit.h>
#import "UILabel+MYFLabel.h"
#import <objc/runtime.h>

@interface UILabel (MYFLabel)
@property (strong, nonatomic) UIColor *nightBackgroundColor;
@end
@implementation UILabel (MYFLabel)
- (UIColor *)nightBackgroundColor{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setNightBackgroundColor:(UIColor *)nightBackgroundColor{
    objc_setAssociatedObject(self, @selector(nightBackgroundColor),nightBackgroundColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    self.backgroundColor = self.nightBackgroundColor;
}
@end

使用

UILabel *nameLabel = [[UILabel alloc]initWithFrame:self.view.bounds];
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.text = @"慕云飞";
nameLabel.nightBackgroundColor = [UIColor redColor];
[self.view addSubview:nameLabel];

结果

Paste_Image.png

相关文章

  • 运行时小探

    一个类(Class)维护一张调度表(dispatch table)用于解析运行时发送的消息;调度表中的每个实体(e...

  • 圣诞树蛋糕包,钩织完成(附图解)~

    今天早上一醒来,小甜甜爬起来就说“细探拉银”,“细探拉银书”,“细探拉银在哪里”,“甜甜细探拉银”。 小甜甜轻声细...

  • 《寺庙小探》

    如果病入膏肓 苦口良药有什么用呢? 如果飞灰湮灭 祈求祷告有什么用呢? 如果钱财散尽 哭天喊地有什么用呢? 如果万...

  • 小探班

    前天周六,孩子爸爸由于两会,需要加班备勤。虽然是加班,但是其实并没有很重大的任务,于是孩子爸爸提议把娃带到单位去,...

  • 白居易小探

    卖炭翁,伐薪烧炭南山中。 满面尘灰烟火色,两鬓苍苍十指黑。 …… 学生们在大声地背诵白居易的《卖炭翁》…… 忽然兴...

  • 小探澎湃

    这个社会的运转不需要多余的情绪,只需要结果。 “有一次去残疾人机构做志愿者,有些心理承受能力差的人去到反应很大,一...

  • 章草小探

    早就听过书法高手说,写章草的好处说也说不完。前段时间在网上看到了张旭光先生的说法,他具体的说了一下。正常的好处大概...

  • Runtime触探学习笔记第一篇

    Runtime触探学习笔记 runtime是一套API,由C和C++汇编一起写成的API,给我们的OC提供运行时....

  • 这样的豆沙包 差评!!!

    Hi亲爱的朋友! 欢迎来到小厨探食,我是C小探! 今天跟大家分享被C小探差评的豆沙包,前面9步都对的就错在...

  • 一盘任意丝炒米粉,馋哭隔壁家的老公!

    Hi亲爱的朋友!欢迎来到小厨探食!我是C小探! 三丝米粉相信大家都吃过,今天C小探要给大家做的是一道任意丝米粉。米...

网友评论

    本文标题:运行时小探

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