Skip to content

Instantly share code, notes, and snippets.

@rudyjahchan
Created March 25, 2012 05:55
Show Gist options
  • Save rudyjahchan/2191625 to your computer and use it in GitHub Desktop.
Save rudyjahchan/2191625 to your computer and use it in GitHub Desktop.
Monkey-Patching iOS with Objective-C Categories Part II: Adding Properties
#import "MyClassWithFakePrivateProperties.h"
@interface MyClassWithFakePrivateProperties ()
@property (nonatomic, strong) NSString *foo;
@end
@implementation MyClassWithFakePrivateProperties
@synthesize foo;
@end
#import <UIKit/UIKit.h>
@interface UIViewController (TourGuide)
@property (nonatomic, retain) NSArray* tourSteps;
- (void) displayTour;
@end
#import "UIViewController_TourGuide.h"
static char tourStepsKey;
@implementation UIViewController (TourGuide)
- (void) setTourSteps:(NSArray*)tourSteps {
objc_setAssociatedObject(self, &tourStepsKey,
tourSteps, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSArray*) getTourSteps {
return objc_getAssociatedObject(self, &tourStepsKey);
}
- (void) displayTour {
// run the tour
}
@end
#import "UIViewController_TourGuide.h"
@implementation UIViewController (TourGuide)
@synthesize tourSteps = tourSteps_
- (void) displayTour {
// run the tour
}
@end
#import <UIKit/UIKit.h>
@interface UIViewController (TourGuide) {
@private
NSArray* _tourSteps;
}
@property (nonatomic, retain) NSArray* tourSteps;
- (void) displayTour;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment