iOS开发-关于Associated Objects

一、前言

Associated Objects(关联对象)是什么?什么时候用?为什么要用?怎么用?
最开始用到关联对象是源于一个需求(废话,肯定是源于需求)。
大家都知道,Button的点击事件,一定是将本身传入参数:

- (void)setupFoundationUI {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = (CGRect){0, 0, 30, 30};
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnDidClick:(UIButton *)sender {
    NSLog(@"Btn did click ...");
}

如果想要传入一个特定的参数呢?

  • 当时我想传的参数是整型,于是我想到了tag(那时的我还不知道关联对象)
  • tag其实是用来标记不同的Button对象,但此时,我也很无奈。。。就先借用一下吧,哈哈
    也就是这样👇👇👇
- (void)setupFoundationUI {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = (CGRect){0, 0, 30, 30};
    btn.tag = 110;
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnDidClick:(UIButton *)sender {
    NSLog(@"Btn did click ...tag = %zd", sender.tag);
}

后来又有需求,要传的参数是字符串,甚至是对象。。。
就在此时我注意到了关联对象(Associated Objects)

二、关联对象的介绍

1.关联对象解决的问题

我们知道,在 Objective-C 中可以通过 Category (类别、分类,反正你们懂得)给一个现有的类添加属性,但是却不能添加实例变量,这似乎成为了 Objective-C 的一个明显短板,关联对象就可以解决这个问题。

2.如何用关联对象

  • 首先要引入 runtime
#import <objc/runtime.h>
  • API主要就是(来自系统文件runtime.h的介绍)👇👇👇
/** 
 * Sets an associated value for a given object using a given key and association policy.
 * 
 * @param object The source object for the association.
 * @param key The key for the association.
 * @param value The value to associate with the key key for object. Pass nil to clear an existing association.
 * @param policy The policy for the association. For possible values, see “Associative Object Behaviors.”
 * 
 * @see objc_setAssociatedObject
 * @see objc_removeAssociatedObjects
 */
OBJC_EXPORT void
objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
                         id _Nullable value, objc_AssociationPolicy policy)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);

/** 
 * Returns the value associated with a given object for a given key.
 * 
 * @param object The source object for the association.
 * @param key The key for the association.
 * 
 * @return The value associated with the key \e key for \e object.
 * 
 * @see objc_setAssociatedObject
 */
OBJC_EXPORT id _Nullable
objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);

/** 
 * Removes all associations for a given object.
 * 
 * @param object An object that maintains associated objects.
 * 
 * @note The main purpose of this function is to make it easy to return an object 
 *  to a "pristine state”. You should not use this function for general removal of
 *  associations from objects, since it also removes associations that other clients
 *  may have added to the object. Typically you should use \c objc_setAssociatedObject 
 *  with a nil value to clear an association.
 * 
 * @see objc_setAssociatedObject
 * @see objc_getAssociatedObject
 */
OBJC_EXPORT void
objc_removeAssociatedObjects(id _Nonnull object)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);

官方的解释已经很清晰了,就不过多解读了(绑定、获取、移除),值得注意的一点是:objc_removeAssociatedObjects是移除一个对象的所有关联对象,将该对象恢复成“原始”状态,这样的操作风险太大,所以一般的做法是通过给 objc_setAssociatedObject 函数传入 nil 来移除某个已有的关联对象。如下这样👇👇👇

objc_setAssociatedObject(self, &key, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  • 关于key的问题
  • 声明 static char kAssociatedObjectKey; 使用 &kAssociatedObjectKey 作为 key 值;
  • 声明 static void *kAssociatedObjectKey = &kAssociatedObjectKey; 使用 kAssociatedObjectKey 作为 key 值;
  • 用 selector ,使用 getter 方法的名称作为 key 值。
  • 关于policy(关联策略)的问题

OBJC_ASSOCIATION_ASSIGN
等价属性@property (assign) or @property (unsafe_unretained)
弱引用关联对象

OBJC_ASSOCIATION_RETAIN_NONATOMIC
等价属性@property (strong, nonatomic)
强引用关联对象,且为非原子操作

OBJC_ASSOCIATION_COPY_NONATOMIC
等价属性@property (copy, nonatomic)
复制关联对象,且为非原子操作

OBJC_ASSOCIATION_RETAIN
等价属性@property (strong, atomic)
强引用关联对象,且为原子操作

OBJC_ASSOCIATION_COPY
等价属性@property (copy, atomic)
复制关联对象,且为原子操作

具体内容可以参考官方文档,这里就不copy了

三、用关联对象解决上述问题

  • 传整型数据
NSString *const kButtonKey = @"kButtonKey";

- (void)setupFoundationUI {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    objc_setAssociatedObject(btn, &kButtonKey, @110, OBJC_ASSOCIATION_ASSIGN);
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnDidClick:(UIButton *)sender {
    NSInteger value = [objc_getAssociatedObject(sender, &kButtonKey) integerValue];
    NSLog(@"btn did click ...value = %zd", value);
}
  • 传对象数据
NSString *const kButtonKey = @"kButtonKey";

- (void)setupFoundationUI {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = (CGRect){0, 0, 30, 30};
    Person *person = [[Person alloc] init];
    person.name = @"LiMing";
    objc_setAssociatedObject(btn, &kButtonKey, person, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnDidClick:(UIButton *)sender {
    Person *person = objc_getAssociatedObject(sender, &kButtonKey);
    NSLog(@"person`s name = %@", person.name);
}

四、关联对象用于Category

以实现UIBarButtonItem的扩展为例子,为其增加红点的功能,其中大量的使用了关联对象
需求:

1.显示小红点
2.显示数字红点
3.即有小红点又有数字红点时,优先显示数字红点
4.可自定义红点颜色(默认是红色[UIColor redColor])
5.数字红点数目大于99时,显示99+

具体代码如下:👇👇👇

#import <UIKit/UIKit.h>

@interface UIBarButtonItem (Badge)

@property (assign, nonatomic) UIColor *badgeColor;

- (void)configBadgeWithBigNum:(NSInteger)bigNum small:(BOOL)isOn;

@end
#import "UIBarButtonItem+Badge.h"
#import <objc/runtime.h>

NSString *const ZYBarButtonItem_hasBadgeKey = @"ZYBarButtonItem_hasBadgeKey";
NSString *const ZYBarButtonItem_badgeKey = @"ZYBarButtonItem_badgeKey";
NSString *const ZYBarButtonItem_badgeSizeKey = @"ZYBarButtonItem_badgeSizeKey";
NSString *const ZYBarButtonItem_badgeOriginXKey = @"ZYBarButtonItem_badgeOriginXKey";
NSString *const ZYBarButtonItem_badgeOriginYKey = @"ZYBarButtonItem_badgeOriginYKey";
NSString *const ZYBarButtonItem_badgeColorKey = @"ZYBarButtonItem_badgeColorKey";
NSString *const ZYBarButtonItem_badgeSizeWKey = @"ZYBarButtonItem_badgeSizeWKey";

@interface UIBarButtonItem ()

@property (nonatomic, assign) CGFloat badgeSizeW;
@property (strong, nonatomic) UILabel *badge;
@property (assign, nonatomic) CGFloat badgeOriginX;
@property (assign, nonatomic) CGFloat badgeOriginY;
@property (assign, nonatomic) CGFloat badgeSize;
@property BOOL hasBadge;

@end

@implementation UIBarButtonItem (Badge)


- (void)initBadge {
    UIView *superview = nil;
    
    if (self.customView) {
        superview = self.customView;
        superview.clipsToBounds = NO;
    } else if ([self respondsToSelector:@selector(view)] && [(id)self view]) {
        superview = [(id)self view];
    }
    [superview addSubview:self.badge];
    
    // 默认设置 default configure
    self.badgeColor = [UIColor redColor];
    self.badgeSize = 10;
    self.badgeSizeW = 10;
    self.badgeOriginX = 28;
    self.badgeOriginY = 8;
    self.badge.hidden = YES;
    self.badge.layer.masksToBounds = YES;
    self.badge.font = [UIFont boldSystemFontOfSize:12];
    self.badge.textAlignment = NSTextAlignmentCenter;
    self.badge.textColor = [UIColor whiteColor];
}

- (void)showBadge {
    self.badge.hidden = NO;
}

- (void)hideBadge {
    self.badge.hidden = YES;
}

- (void)refreshBadge {
    self.badge.frame = (CGRect){self.badgeOriginX,self.badgeOriginY,self.badgeSizeW,self.badgeSize};
    self.badge.backgroundColor = self.badgeColor;
    self.badge.layer.cornerRadius = self.badgeSize/2;
}


#pragma mark ---------- badge getter & setter function -----------

- (UILabel *)badge {
    UILabel *badge = (UILabel *)objc_getAssociatedObject(self, &ZYBarButtonItem_badgeKey);
    if (!badge) {
        badge = [[UILabel alloc] init];
        [self setBadge:badge];
        [self initBadge];
    }
    return badge;
}

- (void)setBadge:(UILabel *)badge {
    objc_setAssociatedObject(self, &ZYBarButtonItem_badgeKey, badge, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIColor *)badgeColor {
    return objc_getAssociatedObject(self, &ZYBarButtonItem_badgeColorKey);
}

- (void)setBadgeColor:(UIColor *)badgeColor {
    objc_setAssociatedObject(self, &ZYBarButtonItem_badgeColorKey, badgeColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    if (self.badge) {
        [self refreshBadge];
    }
}

-(CGFloat)badgeSize {
    NSNumber *number = objc_getAssociatedObject(self, &ZYBarButtonItem_badgeSizeKey);
    return number.floatValue;
}

-(void)setBadgeSize:(CGFloat)badgeSize {
    NSNumber *number = [NSNumber numberWithDouble:badgeSize];
    objc_setAssociatedObject(self, &ZYBarButtonItem_badgeSizeKey, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    if (self.badge) {
        [self refreshBadge];
    }
}

- (CGFloat)badgeSizeW {
    NSNumber *number = objc_getAssociatedObject(self, &ZYBarButtonItem_badgeSizeWKey);
    return number.floatValue;
}

- (void)setBadgeSizeW:(CGFloat)badgeSizeW {
    NSNumber *number = [NSNumber numberWithDouble:badgeSizeW];
    objc_setAssociatedObject(self, &ZYBarButtonItem_badgeSizeWKey, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    if (self.badge) {
        [self refreshBadge];
    }
}

-(CGFloat)badgeOriginX {
    NSNumber *number = objc_getAssociatedObject(self, &ZYBarButtonItem_badgeOriginXKey);
    return number.floatValue;
}

-(void)setBadgeOriginX:(CGFloat)badgeOriginX {
    NSNumber *number = [NSNumber numberWithDouble:badgeOriginX];
    objc_setAssociatedObject(self, &ZYBarButtonItem_badgeOriginXKey, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    if (self.badge) {
        [self refreshBadge];
    }
}

-(CGFloat)badgeOriginY {
    NSNumber *number = objc_getAssociatedObject(self, &ZYBarButtonItem_badgeOriginYKey);
    return number.floatValue;
}

-(void)setBadgeOriginY:(CGFloat)badgeOriginY {
    NSNumber *number = [NSNumber numberWithDouble:badgeOriginY];
    objc_setAssociatedObject(self, &ZYBarButtonItem_badgeOriginYKey, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    if (self.badge) {
        [self refreshBadge];
    }
}

- (void)setHasBadge:(BOOL)hasBadge {
    if (hasBadge) {
        [self showBadge];
    }else{
        [self hideBadge];
    }
    
    NSNumber *number = [NSNumber numberWithBool:hasBadge];
    objc_setAssociatedObject(self, &ZYBarButtonItem_hasBadgeKey, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)hasBadge {
    NSNumber *number = objc_getAssociatedObject(self, &ZYBarButtonItem_hasBadgeKey);
    return number.boolValue;
}

#pragma mark - Public

- (void)configBadgeWithBigNum:(NSInteger)bigNum small:(BOOL)isOn {
    
    if (bigNum > 0) {
        self.hasBadge = YES;
        self.badgeSize = 18;
        self.badgeOriginY = 6;
        NSString *numStr = [NSString stringWithFormat:@"%zd", bigNum];
        if (bigNum < 10) {
            self.badgeSizeW = 18;
        } else if (bigNum < 100) {
            self.badgeSizeW = 25;
        } else {
            self.badgeSizeW = 30;
            numStr = @"99+";
        }
        self.badge.text = numStr;
    } else if (isOn) {
        self.hasBadge = YES;
        self.badgeSizeW = 10;
        self.badgeSize = 10;
        self.badgeOriginY = 8;
        self.badge.text = nil;
    } else {
        self.hasBadge = NO;
    }
}
@end

五、写在最后

  • 关联对象与被关联对象本身的存储并没有直接的关系,它是存储在单独的哈希表中的;
  • 关联对象的五种关联策略与属性的限定符非常类似,在绝大多数情况下,我们都会使用 OBJC_ASSOCIATION_RETAIN_NONATOMIC 的关联策略,这可以保证我们持有关联对象;
  • 关联对象的释放时机与移除时机并不总是一致,比如用关联策略 OBJC_ASSOCIATION_ASSIGN 进行关联的对象,很早就已经被释放了,但是并没有被移除,而再使用这个关联对象时就会造成 Crash 。
    Associated.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,793评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,567评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,342评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,825评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,814评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,680评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,033评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,687评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,175评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,668评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,775评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,419评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,020评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,206评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,092评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,510评论 2 343

推荐阅读更多精彩内容