[消息传递之三]-NSInvocation练习

- (void)viewDidLoad {
    [super viewDidLoad];
    id m = [[MyInvoc alloc] init];
    SEL s = @selector(myLen:);
    NSMethodSignature* sign = [MyInvoc instanceMethodSignatureForSelector:s];
    NSInvocation* n = [NSInvocation invocationWithMethodSignature:sign];
    [n setTarget:m];
    [n setSelector:s];
    NSString* w = @"hello world";
    [n setArgument:&w atIndex:2];
    [n invoke];
    NSInteger r ;
    [n getReturnValue:&r];
    NSLog(@"length is %ld",r);
   // NSLog(@"name is %@, length is %ld",[m name], [m length]);
}

1,nsinvocation能传递多个参数,并且能获取返回值,这两点比performselector;withobject要强大。

2,需要配合方法签名使用,幸好这个获取签名的函数在nsobject里面就有~

3,在哪个线程调用,就用哪个线程执行,貌似没有延迟执行方法,这个与performselector有差别~

你可能感兴趣的:(ios,NSInvocation)