当前位置: 代码迷 >> 综合 >> iOS NSInvocation 使用
  详细解决方案

iOS NSInvocation 使用

热度:22   发布时间:2024-01-13 04:38:41.0


NSInvocation使用还是挺简单的,只是还是有很多开发者不知道他是干嘛的,这里做个比较。

NSInvocation能做啥?

在iOS中如果调用某个对象的消息我们经常使用系统提供的performSelector方法

performSelector: withObject:
performSelector:
...

performSelector这玩意是好,但蛋疼的是如果是参数是多个时,咋办?这时NSInvocation就上场了,所以NSInvocation的作用也是用于调用某个对象的消息,且不限制于被调用消息参数的个数。


例子:

-(void)invocation{//方法签名NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:@selector(calWith:andB:)];if (signature) {NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];invocation.target = self;invocation.selector = @selector(calWith:andB:);int a=10,b=20;//自定义参数索引从2开始(atIndex?
  相关解决方案