Created
November 22, 2012 22:49
-
-
Save jinthagerman/4133210 to your computer and use it in GitHub Desktop.
Simply create associated property with one line of code using a reusable preproc macro
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 <objc/runtime.h> | |
#define ASSOCIATIVE_PROPERTY(GET_NAME, SET_NAME, TYPE, ASSOCIATION_POLICY) \ | |
static char GET_NAME##Key; \ | |
- (TYPE) GET_NAME \ | |
{ \ | |
return objc_getAssociatedObject(self, & GET_NAME##Key); \ | |
} \ | |
\ | |
- (void) SET_NAME:(TYPE)value\ | |
{\ | |
objc_setAssociatedObject(self, & GET_NAME##Key, value, ASSOCIATION_POLICY);\ | |
} | |
// Usage | |
// In category @interface | |
@property (nonatomic, retain) NSString* oldString; | |
// In category @implementation | |
ASSOCIATIVE_PROPERTY(oldString, setOldString, NSString*, OBJC_ASSOCIATION_RETAIN) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment