IOS objc_setAssociatedObject/objc_getAssociatedObject
|
category和associative是objective-c的两个扩展机制,category就不介绍了,最常用也就是它。associative是一种发生在运行时的对象关联机制(引入 objc/runtime.h),将一个对象关联到另一对象,通过一个关键字建立关联,这个关键词必须是唯一的,一般采用静态字符串变量才作为关键字。
associative机制共有3个方法,设置关联对象、获取关联对象、删除关联:
/*
*源对象
*关键字:静态变量,可使用static const *NSString 或 staticchar
*关联的对象
*关联策略:assign,retain,copy等协议
*/
OBJC_EXPORTvoid
objc_setAssociatedObject(id object,constvoid
*key,id value,objc_AssociationPolicy policy)
OBJC_EXPORTid
objc_getAssociatedObject(id object,constvoid
*key)
//Removes all associations for a given object.
OBJC_EXPORTvoid
objc_removeAssociatedObjects(id object)
|
通过一个例子来展示associative的用法:
UIView+NodataView.h
// UIView+NodataView.h
// NewIOSProject
// // Created by 罗函 on 16/7/5. // Copyright © 2016年罗函. All rights reserved. //
#import<UIKit/UIKit.h>
@interface UIView (NodataView) - (void)addnodataView:(UIImage *)image text:(NSString *)text hint:(NSString *)hint toppadding:(double)toppadding; - (void)removenodataView; @end
|
UIView+NodataView.m
#import"UIView+NodataView.h"
#import <objc/runtime.h> staticchar ERROR_IMAGE; staticchar ERROR_LABEL; staticchar ERROR_HINT; @implementation UIView (NodataView) - (void)setErrorimageview:(UIImageView
*)errorimageview {
objc_setAssociatedObject(self, &ERROR_IMAGE, errorimageview,OBJC_ASSOCIATION_RETAIN_NONATOMIC); }
- (UIImageView
*)errorimageview {
returnobjc_getAssociatedObject(self, &ERROR_IMAGE); }
- (void)setErrorlabel:(UILabel
*)errorlabel {
objc_setAssociatedObject(self, &ERROR_LABEL, errorlabel,OBJC_ASSOCIATION_RETAIN_NONATOMIC); }
- (UILabel
*)errorlabel {
returnobjc_getAssociatedObject(self, &ERROR_LABEL); }
- (void)setHintlabel:(UILabel
*)hintlabel {
objc_setAssociatedObject(self, &ERROR_HINT, hintlabel,OBJC_ASSOCIATION_RETAIN_NONATOMIC); }
- (UILabel
*)hintlabel {
returnobjc_getAssociatedObject(self, &ERROR_HINT); } - (void)addnodataView:(UIImage *)image text:(NSString *)text hint:(NSString *)hint toppadding:(double)toppadding{ if(!self.errorimageview || !self.errorlabel) { double imagewidth =86.0; double labelheight =30.0; double hintheight =20.0; CGPoint center = [UIApplicationsharedApplication].delegate.window.center; center.y -= (toppadding + imagewidth/2); self.errorimageview = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, imagewidth, imagewidth)]; self.errorimageview.center = center; [selfaddSubview:self.errorimageview]; self.errorlabel = [[UILabelalloc]initWithFrame:CGRectMake(0,self.errorimageview.frame.origin.y+self.errorimageview.frame.size.height,self.bounds.size.width, labelheight)]; [selfaddSubview:self.errorlabel]; self.hintlabel = [[UILabelalloc]initWithFrame:CGRectMake(0,self.errorlabel.frame.origin.y+self.errorlabel.frame.size.height,self.bounds.size.width, hintheight)]; [selfaddSubview:self.hintlabel]; //setup self.errorlabel.font = [UIFontsystemFontOfSize:14.f]; self.errorlabel.textColor = [UIColorcolorWithRed:0green:0blue:0alpha:0.4]; self.errorlabel.textAlignment = NSTextAlignmentCenter;self.errorlabel.font = [UIFontsystemFontOfSize:13.f]; self.hintlabel.font = [UIFontsystemFontOfSize:12.f]; self.hintlabel.textColor = [UIColorcolorWithRed:0green:0blue:0alpha:0.2]; self.hintlabel.textAlignment = NSTextAlignmentCenter; } [self.errorimageviewsetImage:image]; [self.errorlabelsetText:text]; [self.hintlabelsetText:hint]; } - (void)removenodataView { if(self.errorimageview || self.errorlabel ||self.hintlabel) { [self.errorimageviewremoveFromSuperview]; [self.errorlabelremoveFromSuperview]; [self.hintlabelremoveFromSuperview]; self.errorimageview =nil; self.errorlabel =nil; self.hintlabel =nil; } }
@end
|