1.关联是什么
是指动态创建一个指针从一个对象指向另外一个对象,并且遵循相应的“内存管理语义”,相当于动态添加一个属性
2.关联的类型与等效的property属性
关联类型 | 等效的@property属性 |
---|---|
OBJC_ASSOCIATION_ASSIGN | assign |
OBJC_ASSOCIATION_RETAIN_NONATOMIC | nonatomic, retain |
OBJC_ASSOCIATION_COPY_NONATOMIC | nonatomic, copy |
OBJC_ASSOCIATION_RETAIN | retain |
3.关联的相关api
设置关联对象
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)
4.以静态全局变量作为key
- 设置关联对象的key和NSDictionary中的key不一样。其在术语上属于“不透明的指针”。
- NSDictionary中,两个key如果isEqual方法返回YES,则认为两个key相同。
- 而这里要完全相同。
5.使用关联对象好处在哪里?如果要扩充属性,创建一个子类不就行了?
使用继承的方式扩充属性,则代码具有侵入性。OC中是单继承模式,继承了A就无法继承B,还有另外的C。所以某种特定的情况下,无法使用继承。
更多的是使用分类+关联的方式来扩充一个已经存在的类。这样代码没有侵入性,使用起来更加简单简便。
6.UIImageView+WebCache中使用的关联
UIImageView+WebCache中每次设置新的图片的时候都会将当前imageView正在加载的进程给取消了。而这些进程对象就是通过关联的方式动态关联到imageView上。
//UIImageView+WebCache.m
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock {
[self sd_cancelCurrentImageLoad];
...
if (url) {
__weak UIImageView *wself = self;
id operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
...
}];
[self sd_setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"];
} else {
...
}
}
- (void)sd_cancelCurrentImageLoad {
[self sd_cancelImageLoadOperationWithKey:@"UIImageViewImageLoad"];
}
//UIView+WebCacheOperation.m
- (NSMutableDictionary *)operationDictionary {
NSMutableDictionary *operations = objc_getAssociatedObject(self, &loadOperationKey);
if (operations) {
return operations;
}
operations = [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, &loadOperationKey, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return operations;
}
- (void)sd_setImageLoadOperation:(id)operation forKey:(NSString *)key {
[self sd_cancelImageLoadOperationWithKey:key];
NSMutableDictionary *operationDictionary = [self operationDictionary];
[operationDictionary setObject:operation forKey:key];
}
7.使用关联还可以使 因为delegate形式而变得分散的代码 通过block的形式 集中到一起。这样可读性更强,代码更简洁。
以UIActionSheet为例。
先来一个不使用关联时的代码。
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)didClickBtn:(UIButton *)btn{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Hellow World" delegate:self cancelButtonTitle:@"不hellow" destructiveButtonTitle:@"hellow1" otherButtonTitles:@"hellow2",nil];
[sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"点击了 %ld",buttonIndex);
}
@end
这样点击actionsheet后的逻辑与创建时候的代码是分开的,当代码一多的情况下,这个代理方法和其他的代理方法放在一起,可读性比较差。而且每次创建一个UIActionSheet都需要:
- 设置代理
- 添加代理protocol
- 实现相关代理方法
--
使用关联我们可以将ActionSheet的相关逻辑独立成一个Tool,代码如下
//.m
static char tool;
static char sheet;
@interface YXActionSheetTool ()
@property (nonatomic,copy) void(^callback)(NSInteger clickIndex);
@property (nonatomic,weak) UIViewController *controller;
@end
@implementation YXActionSheetTool
+ (void)showWithController:(UIViewController *)controller title:(NSString *)title cancleTitle:(NSString *)cancleTitle destructiveTitle:(NSString *)destructiveTitle otherTitles:(NSArray *)otherTitles callback:(void(^)(NSInteger clickIndex))callback {
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:title delegate:nil cancelButtonTitle:cancleTitle destructiveButtonTitle:destructiveTitle otherButtonTitles:nil];
for (NSString *otherTitle in otherTitles) {
[sheet addButtonWithTitle:otherTitle];
}
YXActionSheetTool *actionSheetTool = [[YXActionSheetTool alloc] init];
objc_setAssociatedObject(controller,&tool,actionSheetTool,OBJC_ASSOCIATION_RETAIN_NONATOMIC);//防止tool被释放
objc_setAssociatedObject(actionSheetTool,&sheet,sheet,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
actionSheetTool.callback=callback;
actionSheetTool.controller =controller;
sheet.delegate=actionSheetTool;
[sheet showInView:controller.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
objc_setAssociatedObject(self,&sheet,nil,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self.controller,&tool,nil,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if(self.callback)
self.callback(buttonIndex);
}
@end
调用代码如下
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)didClickBtn:(UIButton *)btn{
[YXActionSheetTool showWithController:self title:@"Hellow World" cancleTitle:@"不hellow" destructiveTitle:@"hellow1" otherTitles:@[@"hellow2"] callback:^(NSInteger clickIndex) {
NSLog(@"点击了 %ld",clickIndex);
}];
}
@end
这样就使ui代码和逻辑代码在一起,方便阅读。