Skip to content

Instantly share code, notes, and snippets.

@bonty
Last active December 20, 2015 16:48
Show Gist options
  • Save bonty/6163637 to your computer and use it in GitHub Desktop.
Save bonty/6163637 to your computer and use it in GitHub Desktop.
Categoryで擬似インスタンス変数を設定する
#import <UIKit/UIKit.h>
@interface UIViewController (MyCategory)
@property (nonatomic, strong) NSNumber *myProperty;
@end
#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