寻找严肃、沉默和专注的力量。
分类: iOS平台
2015-03-18 09:32:45
本文转载自:http://www.cnblogs.com/wengzilin/p/4331685.html?utm_source=tuicool
想到要如何为所有的对象增加实例变量吗?我们知道,使用Category可以很方便地为现有的类增加方法,但却无法直接增加实例变量。不过从Mac OS X v10.6开始,系统提供了Associative References,这个问题就很容易解决了。这种方法也就是所谓的关联(association),我们可以在runtime期间动态地添加任意多的属性,并且随时读取。所用到的两个重要runtime API是:
OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
现在我们结合一个实际的例子来说明他们的用法。假设我们现在打算利用category对UILabel进行属性补充,可以这么做:
#import <UIKit/UIKit.h> #import <objc/runtime.h> @interface UILabel (Associate) - (void) setFlashColor:(UIColor *) flashColor; - (UIColor *) getFlashColor; @end
#import "UILabel+Associate.h"
@implementation UILabel (Associate)
static char flashColorKey;
- (void) setFlashColor:(UIColor *) flashColor { objc_setAssociatedObject(self, &flashColorKey, flashColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (UIColor *) getFlashColor {
return objc_getAssociatedObject(self, &flashColorKey);
}
@end
上面的例子有几个需要注意的地方:
1、key:我们注意到在函数签名中key的类型const void *,这表示key仅仅是一个地址,而不是字符串的内容,这也是为说明flashColorKey没有初始化的原因,因为具体指向什么内容我们无所谓,我们要的仅仅是地址!如果在setAssocaitedObject中你传入的是flashColorKey,那get方法得到的值将会是nil。正确的应该是传入地址&flashColorKey。
2、policy:这里的policy跟属性声明中的retain、assign、copy是一样的,不再赘述
下面我们再来看另一个例子,来源于APPLE GUIDE:
#import
#import
int main (int argc, const char * argv[]) { @autoreleasepool {
/*Seciton 0. 关联数据的Key和Value*/
static char overviewKey;
static const char *myOwnKey = "VideoProperty\0";
static const char intValueKey = 'i'; NSArray *array = [[NSArray alloc] initWithObjects:@ "One", @"Two", @"Three", nil];
// For the purposes of illustration, use initWithFormat: to ensure // we get a deallocatable string
NSString *overview = [[NSString alloc] initWithFormat:@"%@", @"First three numbers"];
NSString *videoKeyValue = @"This is a video";
NSNumber *intValue = [[NSNumber alloc] initWithInt:5];
/*Section 1. 关联数据设置部分*/
objc_setAssociatedObject ( array, &overviewKey, overview, OBJC_ASSOCIATION_RETAIN );
[overview release];
objc_setAssociatedObject ( array, myOwnKey, videoKeyValue, OBJC_ASSOCIATION_RETAIN );
objc_setAssociatedObject ( array, &intValueKey, intValue, OBJC_ASSOCIATION_RETAIN );
/*Section 3. 关联数据查询部分*/
NSString *associatedObject = (NSString *) objc_getAssociatedObject (array, &overviewKey); NSLog(@"associatedObject: %@", associatedObject);
NSString *associatedObject2 = (NSString *) objc_getAssociatedObject(array, myOwnKey);
NSLog(@"Video Key value is %@", associatedObject2);
NSString *assObject3 = (NSString *) objc_getAssociatedObject(array, &myOwnKey);
if( assObject3 ) {
NSLog(@"不会进入这里! assObject3 应当为nil!"); }
else { NSLog(@"OK. 通过myOwnKey的地址是得不到数据的!"); } NSNumber *assKeyValue = (NSNumber *) objc_getAssociatedObject(array, &intValueKey);
NSLog(@"Int value is %d",[assKeyValue intValue]);
/*Section 3. 关联数据清理部分*/
objc_setAssociatedObject ( array, &overviewKey, nil, OBJC_ASSOCIATION_ASSIGN );
objc_setAssociatedObject ( array, myOwnKey, nil, OBJC_ASSOCIATION_ASSIGN );
objc_setAssociatedObject ( array, &intValueKey, nil, OBJC_ASSOCIATION_ASSIGN ); [array release]; } return 0; }
2、kvc的方式
// 对NSObject扩展Simon特有的方法及属性
#import
@interface NSObject (Simon)
// book id
- (NSInteger)bookId;
- (void)setBookId:(NSInteger)_bookId;
// book name
- (NSString *)bookName;
- (void)setBookName:(NSString *)_bookName;
// otherInfo
- (NSDictionary *)otherInfo;
- (void)setOtherInfo:(NSDictionary *)_otherInfo;
@end
#import "NSObject+Simon.h"
@implementation NSObject (Simon)
static NSMutableDictionary *_td = nil;
+ (NSMutableDictionary *)_td{
if (_td == nil) {
_td = [NSMutableDictionary dictionary];
}
return _td;
}
// book id
- (NSInteger)bookId {
return [[[[self class] _td] objectForKey:@"bookId"] intValue];
}
- (void)setBookId:(NSInteger)_bookId {
NSNumber *bookId = [NSNumber numberWithInt:_bookId];
[[[self class] _td] setObject:bookId forKey:@"bookId"];
}
// book name
- (NSString *)bookName{
return [[[self class] _td] objectForKey:@"bookName"];
}
- (void)setBookName:(NSString *)_bookName{
NSString *bookName = [_bookName copy];
[[[self class] _td] setObject:bookName forKey:@"bookName"];
[bookName release];
}
// otherInfo
- (NSDictionary *)otherInfo{
return [[[self class] _td] objectForKey:@"otherInfo"];
}
- (void)setOtherInfo:(NSDictionary *)_otherInfo{
NSDictionary *otherInfo = [_otherInfo copy];
[[[self class] _td] setObject:otherInfo forKey:@"otherInfo"];
[otherInfo release];
}
@end
#import
@interface SFBook : NSObject
@end
#import "SFBook.h"
@implementation SFBook
@end
// 使用这个属性扩展
SFBook *book = [[SFBook alloc] init];
book.bookId = 1;
book.bookName = @"IOS设计模式";
NSLog(@">>>>>%d>>>>>>%@",book.bookId,book.bookName);
[book release];