什么是關(guān)聯(lián)對(duì)象
關(guān)聯(lián)對(duì)象是指某個(gè)OC對(duì)象通過一個(gè)唯一的key連接到一個(gè)類的實(shí)例上行疏。
舉個(gè)例子:xiaoming是Person類的一個(gè)實(shí)例精算,他的dog(一個(gè)OC對(duì)象)通過一根繩子(key)被他牽著散步,這可以說xiaoming和dog是關(guān)聯(lián)起來的续捂,當(dāng)然xiaoming可以牽著多個(gè)dog。
怎樣關(guān)聯(lián)對(duì)象
runtime提供給我們的方法:
//關(guān)聯(lián)對(duì)象
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
//獲取關(guān)聯(lián)的對(duì)象
id objc_getAssociatedObject(id object, const void *key)
//移除關(guān)聯(lián)的對(duì)象
void objc_removeAssociatedObjects(id object)
變量說明:
id object:被關(guān)聯(lián)的對(duì)象(如xiaoming)
const void *key:關(guān)聯(lián)的key,要求唯一
id value:關(guān)聯(lián)的對(duì)象(如dog)
objc_AssociationPolicy policy:內(nèi)存管理的策略
objc_AssociationPolicy policy的enum值有:
OBJC_ASSOCIATION_ASSIGN = 0,
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1,
OBJC_ASSOCIATION_COPY_NONATOMIC = 3,
OBJC_ASSOCIATION_RETAIN = 01401,
OBJC_ASSOCIATION_COPY = 01403
當(dāng)對(duì)象被釋放時(shí)娩鹉,會(huì)根據(jù)這個(gè)策略來決定是否釋放關(guān)聯(lián)的對(duì)象,當(dāng)策略是RETAIN/COPY時(shí)稚伍,會(huì)釋放(release)關(guān)聯(lián)的對(duì)象弯予,當(dāng)是ASSIGN,將不會(huì)釋放个曙。
值得注意的是锈嫩,我們不需要主動(dòng)調(diào)用removeAssociated來接觸關(guān)聯(lián)的對(duì)象,如果需要解除指定的對(duì)象垦搬,可以使用setAssociatedObject置nil來實(shí)現(xiàn)呼寸。
關(guān)聯(lián)對(duì)象的應(yīng)用
1、添加公共屬性
這是最常用的一個(gè)模式猴贰,通常我們會(huì)在類聲明里面添加屬性对雪,但是出于某些需求(如前言描述的情況),我們需要在分類里添加一個(gè)或多個(gè)屬性的話米绕,編譯器就會(huì)報(bào)錯(cuò)瑟捣,這個(gè)問題的解決方案就是使用runtime的關(guān)聯(lián)對(duì)象。
應(yīng)用舉例:
我們需要自定義一個(gè)tabbar义郑,并暴露公共的屬性和方法蝶柿。(讀者們可以思考下使用繼承和分類實(shí)現(xiàn)的優(yōu)點(diǎn)和不足之處)
@interface UITabBarController (Custom)
@property (nonatomic, strong) SUCustomTabbar * customTabbar;
@end
#import "UITabBarController+Custom.h"
#import <objc/runtime.h>
@implementation UITabBarController (Custom)
- (void)setCustomTabbar:(UIView *)customTabbar {
//這里使用方法的指針地址作為唯一的key
objc_setAssociatedObject(self, @selector(customTabbar), customTabbar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)customTabbar {
return objc_getAssociatedObject(self, @selector(customTabbar));
}
//其他方法...
@end
這樣,我們就可以像原生的tabbar一樣使用自定義的tabbar:
[self.tabBarController.customTabbar doSomgthig];
例如在iOS中我們都是用過UIAlert類非驮,當(dāng)用戶要處理點(diǎn)擊事件的時(shí)候交汤,需要通過委托協(xié)議來實(shí)現(xiàn)。這時(shí)候就需要把視圖和事先動(dòng)作的代碼分開劫笙。例如:
-(void)userAlert {
UIAlert *alert = [[UIAlert alloc] initWithTitle:@"Alert"
message:@"do you want to close?"
delegate: self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK",nil];
[alert show];
}
#param -mark UIAlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger:)buttonIndex{
if(buttonIndex == 0){
//do action
} else {
//do action
}
}
通常都是這么做芙扎,但是如果代碼中使用多個(gè)UIAlerView的時(shí)候,還需要通過在回調(diào)中判斷alertView的類型填大,然后再去處理響應(yīng)的邏輯戒洼。要是能夠是創(chuàng)建視圖的時(shí)候,就把每個(gè)按鈕響應(yīng)的邏輯寫好允华,那就簡(jiǎn)單多了圈浇。于是可以:
_alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is deprecated?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok", nil];
void (^block)(NSInteger) = ^(NSInteger buttonIndex){
if (buttonIndex == 0) {
[self doCancel];
} else {
[self doOk];
}
};
objc_setAssociatedObject(self.alertView, MyAlertViewKey, block, OBJC_ASSOCIATION_COPY);
#pragma -mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
void (^block)(NSInteger) = objc_getAssociatedObject(alertView, MyAlertViewKey);
block(buttonIndex);
}
2寥掐、添加私有成員變量
有時(shí)候,需要在分類中添加不想暴露在公共聲明的成員變量磷蜀。
應(yīng)用舉例:給按鈕添加點(diǎn)擊時(shí)間的回調(diào)
@interface UIButton (Callback)
- (instancetype)initWithFrame:(CGRect)frame callback:(void (^)(UIButton *))callbackBlock;
@end
@interface UIButton ()
@property (nonatomic, copy) void (^callbackBlock)(UIButton * button);
@end
@implementation UIButton (Callback)
- (void (^)(UIButton *))callbackBlock {
return objc_getAssociatedObject(self, @selector(callbackBlock));
}
- (void)setCallbackBlock:(void (^)(UIButton *))callbackBlock {
objc_setAssociatedObject(self, @selector(callbackBlock), callbackBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (instancetype)initWithFrame:(CGRect)frame callback:(void (^)(UIButton *))callbackBlock {
if (self = [super initWithFrame:frame]) {
self.callbackBlock = callbackBlock;
[self addTarget:self action:@selector(didClickAction:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)didClickAction:(UIButton *)button {
self.callbackBlock(button);
}
@end