Created
September 24, 2012 07:13
-
-
Save Daij-Djan/3774720 to your computer and use it in GitHub Desktop.
easy macros for ObjC properties in categories using AssociativeStorage!
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
//got from stackoverflow - quite nice :) | |
//http://stackoverflow.com/questions/8870197/adding-properties-to-uiviewcontroller-and-all-its-subclasses | |
#ifndef ASSOCIATED_STORAGE_PROPERTY_IMP | |
#define THREE_WAY_PASTER_INNER(a, b, c) a ## b ## c | |
#define THREE_WAY_PASTER(x,y,z) THREE_WAY_PASTER_INNER(x,y,z) | |
#define ASSOCIATED_STORAGE_PROPERTY_IMP(type, setter, getter, policy) \ | |
static void * const THREE_WAY_PASTER(__ASSOCIATED_STORAGE_KEY_, getter, __LINE__) = (void*)&THREE_WAY_PASTER(__ASSOCIATED_STORAGE_KEY_, getter,__LINE__); \ | |
\ | |
- (type)getter { return objc_getAssociatedObject(self, THREE_WAY_PASTER(__ASSOCIATED_STORAGE_KEY_, getter,__LINE__) ); } \ | |
\ | |
- (void)setter: (type)value { objc_setAssociatedObject(self, THREE_WAY_PASTER(__ASSOCIATED_STORAGE_KEY_, getter,__LINE__) , value, policy); } \ | |
#endif | |
//=== | |
#import <objc/runtime.h> | |
@interface UIViewController (SOAdditions) | |
@property (atomic, readwrite, copy) NSString* myProperty; | |
@end | |
@implementation UIViewController (SOAdditions) | |
ASSOCIATED_STORAGE_PROPERTY_IMP(NSString*, setMyProperty, myProperty, OBJC_ASSOCIATION_COPY) | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment