Skip to content

Instantly share code, notes, and snippets.

@hamrickdavid
Created February 12, 2012 21:09
Show Gist options
  • Save hamrickdavid/1810860 to your computer and use it in GitHub Desktop.
Save hamrickdavid/1810860 to your computer and use it in GitHub Desktop.
Adding Properties to an Objective-C Category
#import UIViewDHStyleManager.h"
UIView* v = [[[UIView alloc] init] autorelease];
v.styleName = @"someStyleName";
NSLog(@"v = %@",v.styleName); //Logs 'someStyleName'
@interface UIView (DHStyleManager)
@property (nonatomic, copy) NSString* styleName;
@end
#import "UIViewDHStyleManager.h"
NSString * const kDHStyleKey = @"kDHStyleKey";
@implementation UIView (DHStyleManager)
- (void)setStyleName:(NSString *)styleName
{
objc_setAssociatedObject(self, kDHStyleKey, styleName, OBJC_ASSOCIATION_COPY);
}
- (NSString*)styleName
{
return objc_getAssociatedObject(self, kDHStyleKey);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment