程序员

运行时小探

2016-08-06  本文已影响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] ****慕云飞

替换系统的发表方法

#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>**

我将 .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
上一篇 下一篇

猜你喜欢

热点阅读