#import <objc/runtime.h>
예제1)
// set
{
objc_setAssociatedObject(self, @"ShouldDismissModalViewLater", modalViewController, OBJC_ASSOCIATION_ASSIGN);
}
// get and using
{
UIViewController *modalVC = objc_getAssociatedObject(self, @"ShouldDismissModalViewLater");
[modalVC dismissViewControllerAnimated:YES completion:nil];
}
예제2)
// set
{
NSArray *gotArray;
objc_setAssociatedObject(self, @"MyGotArray", gotArray, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
// get and using
{
NSArray *array = objc_getAssociatedObject(self, @"MyGotArray");
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
}];
}
예제3)
Easier syntax
Alternatively you could use
@selector(nameOfGetter)
instead of creating a static pointer. Why? See http://stackoverflow.com/a/16020927/202451. Example:- (NSArray *)laserUnicorns {
return objc_getAssociatedObject(self, @selector(laserUnicorns));
}
- (void)setLaserUnicorns:(NSArray *)unicorns {
objc_setAssociatedObject(self, @selector(laserUnicorns), unicorns, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
댓글 없음:
댓글 쓰기