public
Created

Simply create associated property with one line of code using a reusable preproc macro

  • Download Gist
APMacros.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.