您的位置:首页 > 其它

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

2016-01-09 08:17 190 查看
- (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有差别~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: