php和ios有啥关联,iOS对象关联

什么是关联对象

关联对象是指某个OC对象通过一个唯一的key连接到一个类的实例上。

举个例子:xiaoming是Person类的一个实例,他的dog(一个OC对象)通过一根绳子(key)被他牵着散步,这可以说xiaoming和dog是关联起来的,当然xiaoming可以牵着多个dog。

怎样关联对象

runtime提供給我们的方法:

//关联对象

void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)

//获取关联的对象

id objc_getAssociatedObject(id object, const void *key)

//移除关联的对象

void objc_removeAssociatedObjects(id object)

变量说明:

id object:被关联的对象(如xiaoming)

const void *key:关联的key,要求唯一

id value:关联的对象(如dog)

objc_AssociationPolicy policy:内存管理的策略

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

当对象被释放时,会根据这个策略来决定是否释放关联的对象,当策略是RETAIN/COPY时,会释放(release)关联的对象,当是ASSIGN,将不会释放。

值得注意的是,我们不需要主动调用removeAssociated来接触关联的对象,如果需要解除指定的对象,可以使用setAssociatedObject置nil来实现。

关联对象的应用

1、添加公共属性

这是最常用的一个模式,通常我们会在类声明里面添加属性,但是出于某些需求(如前言描述的情况),我们需要在分类里添加一个或多个属性的话,编译器就会报错,这个问题的解决方案就是使用runtime的关联对象。

应用举例:

我们需要自定义一个tabbar,并暴露公共的属性和方法。(读者们可以思考下使用继承和分类实现的优点和不足之处)

@interface UITabBarController (Custom)

@property (nonatomic, strong) SUCustomTabbar * customTabbar;

@end

#import "UITabBarController+Custom.h"

#import

@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类,当用户要处理点击事件的时候,需要通过委托协议来实现。这时候就需要把视图和事先动作的代码分开。例如:

-(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

}

}

通常都是这么做,但是如果代码中使用多个UIAlerView的时候,还需要通过在回调中判断alertView的类型,然后再去处理响应的逻辑。要是能够是创建视图的时候,就把每个按钮响应的逻辑写好,那就简单多了。于是可以:

_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、添加私有成员变量

有时候,需要在分类中添加不想暴露在公共声明的成员变量。

应用举例:給按钮添加点击时间的回调

@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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值