Skip to content

Instantly share code, notes, and snippets.

@troyharris
Last active December 19, 2015 03:39
Show Gist options
  • Save troyharris/5891597 to your computer and use it in GitHub Desktop.
Save troyharris/5891597 to your computer and use it in GitHub Desktop.
Example of how to add properties to categories (Thank you: http://www.davidhamrick.com/2012/02/12/Adding-Properties-to-an-Objective-C-Category.html)
// THGridMenuItem+ProjectItem.h
#import "THGridMenuItem.h"
#import "Project.h"
@interface THGridMenuItem (ProjectItem)
@property (nonatomic, strong) Project *project;
-(Project *)project;
-(void)setProject:(Project *)project;
@end
// THGridMenuItem+ProjectItem.m
#import "THGridMenuItem+ProjectItem.h"
#import <objc/runtime.h>
static char projectMenuItemKey;
@implementation THGridMenuItem (ProjectItem)
-(Project *)project {
return objc_getAssociatedObject(self, &projectMenuItemKey);
}
-(void)setProject:(Project *)project {
objc_setAssociatedObject(self, &projectMenuItemKey, project, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment