1.第一种:这种方法最多只能接受两个参数
- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
[self performSelector:@selector(selector1:) withObject:@"123" afterDelay:1];
-(void)selector1:(NSString*)object{
NSLog(@"参数= %@",object);
}
//第二种方法
NSString* str1 =@"a";
NSString* str2 =@"b";
NSString* str3 =@"c";
NSMethodSignature* sign = [selfmethodSignatureForSelector:@selector(personInfo:age:gender:)];
NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:sign];
[invocationsetTarget:self];
[invocationsetSelector:@selector(personInfo:age:gender:)];
[invocationsetArgument:&str1atIndex:2];
[invocationsetArgument:&str2atIndex:3];
[invocationsetArgument:&str3atIndex:4];
[invocation invoke];
-(void)personInfo:(NSString*)info age:(NSString*)age gender:(NSString*)gender{
NSLog(@"info = %@ age = %@ gender = %@",info,age,gender);
}
注意:参数是从第二个位置开始的,因为0为self,1位为selector
(lldb) po invocation
<NSInvocation: 0x60000027e980>
return value: {v} void
target: {@} 0x7fd1bd71ca30
selector: {:} personInfo:age:gender:
argument 2: {@} 0x103c073e0
argument 3: {@} 0x103c07400
argument 4: {@} 0x103c07420
网友评论