在iOS中,使用performSelector方法可以调用一个方法,但是只能传递一个参数。如果需要传递多个参数,可以通过将参数封装为一个对象的方式来实现。
以下是一个示例代码:
- (void)methodWithMultipleParameters:(NSString *)param1 param2:(NSInteger)param2 {NSLog(@"param1 = %@, param2 = %ld", param1, (long)param2);}- (void)performSelectorWithMultipleParameters {NSString *param1 = @"Hello";NSInteger param2 = 123;NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(methodWithMultipleParameters:param2:)]];[invocation setTarget:self];[invocation setSelector:@selector(methodWithMultipleParameters:param2:)];[invocation setArgument:¶m1 atIndex:2];[invocation setArgument:¶m2 atIndex:3];[invocation invoke];}在performSelectorWithMultipleParameters方法中,我们首先准备需要传递的参数param1和param2。然后通过NSInvocation的方式来调用方法methodWithMultipleParameters:param2:。我们使用methodSignatureForSelector方法获取方法的签名,然后创建一个NSInvocation对象并设置target、selector和参数。最后通过invoke方法来调用方法。
在methodWithMultipleParameters:param2:方法中,我们可以打印出传递的参数值。
参考文档:
NSInvocation
Method Signatures

