生活随笔
收集整理的這篇文章主要介紹了
iOS对象关联
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
什么是關(guān)聯(lián)對(duì)象
關(guān)聯(lián)對(duì)象是指某個(gè)OC對(duì)象通過(guò)一個(gè)唯一的key連接到一個(gè)類的實(shí)例上。
舉個(gè)例子:xiaoming是Person類的一個(gè)實(shí)例,他的dog(一個(gè)OC對(duì)象)通過(guò)一根繩子(key)被他牽著散步,這可以說(shuō)xiaoming和dog是關(guān)聯(lián)起來(lái)的,當(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)
復(fù)制代碼變量說(shuō)明:
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
復(fù)制代碼當(dāng)對(duì)象被釋放時(shí),會(huì)根據(jù)這個(gè)策略來(lái)決定是否釋放關(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來(lái)接觸關(guān)聯(lián)的對(duì)象,如果需要解除指定的對(duì)象,可以使用setAssociatedObject置nil來(lái)實(shí)現(xiàn)。
關(guān)聯(lián)對(duì)象的應(yīng)用
1、添加公共屬性
這是最常用的一個(gè)模式,通常我們會(huì)在類聲明里面添加屬性,但是出于某些需求(如前言描述的情況),我們需要在分類里添加一個(gè)或多個(gè)屬性的話,編譯器就會(huì)報(bào)錯(cuò),這個(gè)問(wèn)題的解決方案就是使用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
@implementation UITabBarController (Custom)- (void)
setCustomTabbar:(UIView *)customTabbar {//這里使用方法的指針地址作為唯一的keyobjc_setAssociatedObject(self, @selector(customTabbar), customTabbar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}- (UIView *)customTabbar {
return objc_getAssociatedObject(self, @selector(customTabbar));
}//其他方法...@end
復(fù)制代碼這樣,我們就可以像原生的tabbar一樣使用自定義的tabbar:
[self.tabBarController.customTabbar
doSomgthig];
復(fù)制代碼例如在iOS中我們都是用過(guò)UIAlert類,當(dāng)用戶要處理點(diǎn)擊事件的時(shí)候,需要通過(guò)委托協(xié)議來(lái)實(shí)現(xiàn)。這時(shí)候就需要把視圖和事先動(dòng)作的代碼分開。例如:
-(void)userAlert {UIAlert *alert = [[UIAlert alloc] initWithTitle:@
"Alert" message:@
"do you want to close?" delegate: selfcancelButtonTitle:@
"Cancel"otherButtonTitles:@
"OK",nil];[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger:)buttonIndex{
if(buttonIndex == 0){//
do action}
else {//
do action}
}
復(fù)制代碼通常都是這么做,但是如果代碼中使用多個(gè)UIAlerView的時(shí)候,還需要通過(guò)在回調(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);
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {void (^block)(NSInteger) = objc_getAssociatedObject(alertView, MyAlertViewKey);block(buttonIndex);
}
復(fù)制代碼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
復(fù)制代碼
總結(jié)
以上是生活随笔為你收集整理的iOS对象关联的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。