Last active
December 20, 2015 16:48
-
-
Save bonty/6163637 to your computer and use it in GitHub Desktop.
Categoryで擬似インスタンス変数を設定する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface UIViewController (MyCategory) | |
@property (nonatomic, strong) NSNumber *myProperty; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "UIViewController+MyCategory.h" | |
#import <objc/runtime.h> | |
static char myPropertyKey; | |
@implementation UIViewController (MyCategory) | |
- (void)setMyProperty:(NSNumber *)myProperty | |
{ | |
objc_setAssociatedObject(self, &myPropertyKey, myProperty, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
- (NSNumber *)myProperty | |
{ | |
return objc_getAssociatedObject(self, &myPropertyKey); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment