运行时小探

一个类(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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 5,876评论 0 9
  • 继上Runtime梳理(四) 通过前面的学习,我们了解到Objective-C的动态特性:Objective-C不...
    小名一峰阅读 4,136评论 0 3
  • 我们常常会听说 Objective-C 是一门动态语言,那么这个「动态」表现在哪呢?我想最主要的表现就是 Obje...
    Ethan_Struggle阅读 6,605评论 0 7
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,859评论 0 17
  • 这周李笑来老师得到专栏的主题是关于选择的方法,按照李笑来老师的概括就是:添加必要的条件。文章以剩男剩女的例子开头,...
    逸恒心阅读 1,747评论 0 0

友情链接更多精彩内容