Skip to content

Instantly share code, notes, and snippets.

@jinthagerman
Created November 22, 2012 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jinthagerman/4133210 to your computer and use it in GitHub Desktop.
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
#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