runtime的关联引用


下文会用到const,static关键词,下一篇文章会针对const,static,extern等关键字的用法详细介绍,9月18号之前会找空闲编写上传,敬请期待,同时欢迎大神指点不足之处-----我的愿望是世界充满爱


关于关联引用,简单的应用场景,举个栗子:

(1)给一个类动态添加一个属性;

(2)给一个类添加一个方法;

官方的API是这样子的:

//挂连策略,这是类型枚举,按照字段可以判断用处
typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
    OBJC_ASSOCIATION_ASSIGN = 0,           /**< Specifies a weak reference to the associated object. */
    OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object. 
                                            *   The association is not made atomically. */
    OBJC_ASSOCIATION_COPY_NONATOMIC = 3,   /**< Specifies that the associated object is copied. 
                                            *   The association is not made atomically. */
    OBJC_ASSOCIATION_RETAIN = 01401,       /**< Specifies a strong reference to the associated object.
                                            *   The association is made atomically. */
    OBJC_ASSOCIATION_COPY = 01403          /**< Specifies that the associated object is copied.
                                            *   The association is made atomically. */
};

关联对象的三种用法

/*
object 源对象
key 关键字
value 关联的对象
关键策略 上述的枚举
*/
//设置关联对象
OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);

//获取关联对象
OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)
    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);

//删除关联对象
OBJC_EXPORT void objc_removeAssociatedObjects(id object)
    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);

情景一:动态添加属性

有一个User的类,类里面有关于User的属性,但是后来你要新添加属性,User这个类会经常用到,但是你新添加的属性却不怎么用到,为了节省内存,造成不必要的系统开销,所以做一个动态添加属性

1.创建一个User类,基于NSObject

#import <Foundation/Foundation.h>

@interface User : NSObject

@property(nonatomic,copy)NSString *userName;

@end

2.需要添加一个userAge的属性

创建一个基于User的类别

#import "User.h"

@interface User (Extensions)

@property(nonatomic,copy)NSString *userAge;

@end
#import "User+Extensions.h"
#import <objc/runtime.h>

@implementation User (Extensions)
static char userAgeKey;

-(NSString *)userAge{
    return objc_getAssociatedObject(self,&userAgeKey);
}

-(void)setUserAge:(NSString *)userAge{
    objc_setAssociatedObject(self, &userAgeKey, userAge, OBJC_ASSOCIATION_COPY);
}

@end

这就是最简单的一个使用场景

场景二:给UIViewController添加一个方法

给UIViewController添加一个HUD的方法

1.基于UIViewController创建一个类别,然后声明一个方法

#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"

@interface UIViewController (HUD)

-(void)showHudInView:(UIView *)view
                hint:(NSString *)hint;

@end

2.实现方法

#import "UIViewController+HUD.h"
#import <objc/runtime.h>

static const void * httpReaHud = &httpReaHud;

@implementation UIViewController (HUD)

-(MBProgressHUD *)hud{
    return objc_getAssociatedObject(self, httpReaHud);
}

-(void)setHud:(MBProgressHUD *)hud{
    objc_setAssociatedObject(self, httpReaHud, hud, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(void)showHudInView:(UIView *)view hint:(NSString *)hint{
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:view];
    HUD.label.text = hint;
    [view addSubview:HUD];
    [HUD showAnimated:YES];
    [self setHud:HUD];
}

@end

这样从项目的整洁度,简洁度还有节省内存都有好处

这个是demo,有需要的可以简单的看一下https://github.com/summer7659/runtime-

推荐阅读更多精彩内容