본문 바로가기

Objective-C

[Objective-C] NSInvocation

간단한 예제..



#import <foundation/foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *prose = @"Do nine men interpret?";
NSMethodSignature *signature;
NSInvocation *invocation;
NSString *count;
signature = [prose methodSignatureForSelector:@selector(substringWithRange:)];
invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:@selector(substringWithRange:)];
NSRange range = NSMakeRange(3,4);
[invocation setArgument:&range atIndex:2];
[invocation invokeWithTarget:prose];
[invocation getReturnValue:&count];
NSLog(@"%@ men, I nod.", count);
}
return 0;
}
view raw NSInvocation.m hosted with ❤ by GitHub

NSMethodSignature의 객체를 생성 할 때 셀렉터를 지정하는데 NSInvocation의 객체를 생성 할 때도

셀렉터를 set해줘야 함... signature는 특정 셀렉터와 연결되는 것이 아니라 구성의 정보만 가지고 있는 상태라

그렇다고 함....