Skip to content

Instantly share code, notes, and snippets.

@Kievkao
Created February 28, 2016 20:52
Show Gist options
  • Select an option

  • Save Kievkao/babec52fe2c03e5707b9 to your computer and use it in GitHub Desktop.

Select an option

Save Kievkao/babec52fe2c03e5707b9 to your computer and use it in GitHub Desktop.
Proxying in Objective-C
@interface ChildClass : NSObject
- (NSInteger)read;
@end
// *.h
@interface MyDecorator : NSObject
+ (instancetype)decoratedInstanceOf:(ChildClass *)instance;
@end
// *.m
@interface MyDecorator()
@property (nonatomic, strong) ChildClass *instance;
@end
@implementation MyDecorator
+ (instancetype)decoratedInstanceOf:(ChildClass *)instance {
return [[self alloc] initWithObject:instance];
}
- (instancetype)initWithObject:(ChildClass *)object {
_instance = object;
return self;
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
return self.instance;
}
- (NSInteger)read {
return [self.instance read] + 1;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment