Last active
August 29, 2015 14:10
-
-
Save yuuki1224/26df18821f49542d16d5 to your computer and use it in GitHub Desktop.
NSProxyの使い方Memo
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
#import <Foundation/Foundation.h> | |
@interface ASNDynamicProxy : NSProxy | |
@property (strong, nonatomic) NSObject *targetObject; | |
@end | |
@implementation ASNDynamicProxy | |
// methodがないときにこのメソッドがまず呼ばれる.NSInvalidArgumentExceptionを出す前に rubyでいうmethod_missing | |
- (void)forwardInvocation:(NSInvocation *)invocation | |
{ | |
if (self.targetObject) { | |
{ | |
// ここで前処理いれるなら | |
} | |
[invocation setTarget:self.targetObject]; | |
[invocation invoke]; | |
{ | |
unsigned long length = [[invocation methodSignature] methodReturnLength]; | |
void *buffer = (void *)malloc(length); | |
[invocation getReturnValue:&buffer]; | |
} | |
} | |
} | |
// これがないとforwardInvocation:が呼ばれない. | |
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel | |
{ | |
NSMethodSignature *result; | |
if (self.targetObject) { | |
result = [self.targetObject methodSignatureForSelector:sel]; | |
} else { | |
result = [super methodSignatureForSelector:sel]; | |
} | |
return result; | |
} | |
- (NSString *)description | |
{ | |
return [self.targetObject description]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NSProxyをサブクラスにしている例
MrAlek/AWPagedArray - https://github.com/MrAlek/AWPagedArray
ページネーションを便利にするためのライブラリ