介绍
在OC中要在对象中存放信息,我们需要从对象所属的类中继承一个子类,然后改用这个子类的对象。在不想使用子类的情况下,我们可以使用『关联对象』(Associated Object)来处理问题。
给对象关联其他对象,这些对象通过"键"来区分。在存储对象值的时候,可以设置"存储策略"。
typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
OBJC_ASSOCIATION_ASSIGN = 0, /**< Specifies a weak reference to the associated object. */
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object.
* The association is not made atomically. */
OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< Specifies that the associated object is copied.
* The association is not made atomically. */
OBJC_ASSOCIATION_RETAIN = 01401, /**< Specifies a strong reference to the associated object.
* The association is made atomically. */
OBJC_ASSOCIATION_COPY = 01403 /**< Specifies that the associated object is copied.
* The association is made atomically. */
};
这一点和设置属性相同,我们可以类比@propety来理解。
OBJC_ASSOCIATION_ASSIGN ------------------------- assign
OBJC_ASSOCIATION_RETAIN_NONATOMIC ------------ nonatomic, retain
OBJC_ASSOCIATION_COPY_NONATOMIC -------------- nonatomic,copy
OBJC_ASSOCIATION_RETAIN -------------------------- retain
OBJC_ASSOCIATION_COPY ---------------------------- copy
使用
我们可以使用这个特性给系统的类设置属性
给UIImageView
设置imageURL
#import "objc/runtime.h"
#import "UIImageView+WebCache.h"
static const void *imageURLKey = &imageURLKey;
@implementation UIImageView (WebCache)
- (NSString *)imageURL {
return objc_getAssociatedObject(self, imageURLKey);
}
- (void)setImageURL:(NSString *)imageURL {
objc_setAssociatedObject(self, imageURLKey, imageURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
在使用UIAlertView
时,创建视图与处理按钮动作分开,很不方便阅读,我们可以这样处理。
1.设置"键"值。
static void *AlertViewKey = "AlertViewKey";
2.使用
- (void)askQuestion {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"问题" message:@"你想要做…… ?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
void (^block)(NSInteger) = ^(NSInteger buttonIndex) {
if (buttonIndex == 0) {
[self doCancel];
} else if (buttonIndex == 1) {
[self doContinue];
}
};
objc_setAssociatedObject(alertView, AlertViewKey, block, OBJC_ASSOCIATION_COPY);
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
void (^block)(NSInteger) = objc_getAssociatedObject(alertView, AlertViewKey);
block(buttonIndex);
}
网友评论