Created
May 28, 2012 19:45
-
-
Save hamrickdavid/2820879 to your computer and use it in GitHub Desktop.
Adding Properties to an Objective-C Category Revisted
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
static char kDHStyleKey; | |
@interface UIView (DHStyleManager) | |
@property (nonatomic, copy) NSString* styleName; | |
@end | |
@implementation UIView (DHStyleManager) | |
@dynamic styleName; | |
- (void)setStyleName:(NSString *)styleName | |
{ | |
objc_setAssociatedObject(self, &kDHStyleKey, styleName, OBJC_ASSOCIATION_COPY); | |
} | |
- (NSString*)styleName | |
{ | |
return objc_getAssociatedObject(self, &kDHStyleKey); | |
} | |
@end |
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
@interface UIView (DHStyleManager) | |
@property (nonatomic, copy) NSString* styleName; | |
@end | |
@implementation UIView (DHStyleManager) | |
ADD_DYNAMIC_PROPERTY(NSString*,styleName,setStyleName); | |
@end |
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
#define ADD_DYNAMIC_PROPERTY(PROPERTY_TYPE,PROPERTY_NAME,SETTER_NAME) \ | |
@dynamic PROPERTY_NAME ; \ | |
static char kProperty##PROPERTY_NAME; \ | |
- ( PROPERTY_TYPE ) PROPERTY_NAME \ | |
{ \ | |
return ( PROPERTY_TYPE ) objc_getAssociatedObject(self, &(kProperty##PROPERTY_NAME ) ); \ | |
} \ | |
\ | |
- (void) SETTER_NAME :( PROPERTY_TYPE ) PROPERTY_NAME \ | |
{ \ | |
objc_setAssociatedObject(self, &kProperty##PROPERTY_NAME , PROPERTY_NAME , OBJC_ASSOCIATION_RETAIN); \ | |
} \ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment