标签:style blog color 使用 os width
■ 关联策略表明了相关的对象是通过赋值,保留引用还是复制的方式进行关联的;还有这种关联是原子的还是非原子的。这里的关联策略和声明属性时的很类似。这种关联策略是通过使用预先定义好的常量来表示的。
//唯一静态变量key
static const char xxxxxx;
objc_setAssociatedObject(alert, &associatedkey, message, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_getAssociatedObject(alertView, &associatedkey);
objc_setAssociatedObject(alertView, &associatedkey, nil, OBJC_ASSOCIATION_ASSIGN);
int main(int argc, const char* argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool] alloc init]; static char overviewKey; NSArray *array =[[NSArray alloc] initWidthObjects:@"One", @"Two", @"Three", nil]; //为了演示的目的,这里使用initWithFormat:来确保字符串可以被销毁 NSString * overview = [[NSString alloc] initWithFormat:@"@",@"First three numbers"]; objc_setAssociatedObject(array, &overviewKey, overview, OBJC_ASSOCIATION_RETAIN); [overview release]; NSString *associatedObject = (NSString *)objc_getAssociatedObject(arrray, &overviewKey); NSLog(@"associatedObject:%@", associatedObject); objc_setAssociatedObject(array, &overviewKey, nil, OBJC_ASSOCIATION_ASSIGN); [array release]; [pool drain]; return 0; }
关联objc_setAssociatedObject,objc_getAssociatedObject,布布扣,bubuko.com
关联objc_setAssociatedObject,objc_getAssociatedObject
标签:style blog color 使用 os width
原文地址:http://blog.csdn.net/quanqinyang/article/details/38017581