Objective c 使用参数动态调用方法

Objective c 使用参数动态调用方法,objective-c,ios,Objective C,Ios,我知道可以按如下方式调用方法的名称: NSString *string =@"methodName"; [self performSelector:NSSelectorFromString(string)]; -(void)methodName:(NSString *)_name :withValue (NSString *) value { // todo:..... } 并将执行以下方法: -(void)methodName {

我知道可以按如下方式调用方法的名称:

 NSString *string =@"methodName";
[self performSelector:NSSelectorFromString(string)];
-(void)methodName:(NSString *)_name :withValue (NSString *) value
     {
              // todo:.....
     }
并将执行以下方法:

-(void)methodName
   {
              // todo:.....
   }
我想知道如何调用具有如下参数的方法:

 NSString *string =@"methodName";
[self performSelector:NSSelectorFromString(string)];
-(void)methodName:(NSString *)_name :withValue (NSString *) value
     {
              // todo:.....
     }

您可以使用-performSelector:withObject:

NSDictionary *dic = @{@"name":@"stringName",@"value":@"12"};

NSString *string =@"methodName:";
[self performSelector:NSSelectorFromString(string) withObject:dic];
和在-methodName中:

-(void) methodName:(id)obj
{
    NSDictionary *dic = (NSDictionary *)obj;
    [self methodName:dic[@"name"] withValue:dic[@"value"]];
}

您可以使用-performSelector:withObject:

NSDictionary *dic = @{@"name":@"stringName",@"value":@"12"};

NSString *string =@"methodName:";
[self performSelector:NSSelectorFromString(string) withObject:dic];
和在-methodName中:

-(void) methodName:(id)obj
{
    NSDictionary *dic = (NSDictionary *)obj;
    [self methodName:dic[@"name"] withValue:dic[@"value"]];
}

我不知道确切的语法,但你可以用。

我不知道确切的语法,但你可以用。

我不知道确切需要什么,但从我的理解来看,它是这样的:

NSArray *fruits = [NSArray arrayWithObjects:@"Apple", @"Mango", nil]; 
NSArray *drinks = [NSArray arrayWithObjects:@"Drink1", @"Drink2",nil];
[self serveOrdersWith:fruits andDrinks:drinks];
在类中的某个地方,将使用参数数组调用该方法,即

-(NSArray*)serveOrdersWith :(NSArray*)array1 andDrinks:(NSArray*)array2{
     //Your code here but I will finish with this only
    NSArray *orderArray;
    [orderArray addObject:array1];
    [orderArray addObject:array2];
    return orderArray;
}

我不知道到底需要什么,但根据我的理解,它是这样的:

NSArray *fruits = [NSArray arrayWithObjects:@"Apple", @"Mango", nil]; 
NSArray *drinks = [NSArray arrayWithObjects:@"Drink1", @"Drink2",nil];
[self serveOrdersWith:fruits andDrinks:drinks];
在类中的某个地方,将使用参数数组调用该方法,即

-(NSArray*)serveOrdersWith :(NSArray*)array1 andDrinks:(NSArray*)array2{
     //Your code here but I will finish with this only
    NSArray *orderArray;
    [orderArray addObject:array1];
    [orderArray addObject:array2];
    return orderArray;
}

我不能直接将参数传递给方法?是的,我知道,使用performSelector是不可能的。有关详细信息,请参阅。如果你真的需要多个参数,也许你可以尝试调度。我同意你的答案,在动态编码时,在传递参数时使用key帮助使用dictionary对我很有效,而且最好使用dictionary,因为onevcati不能直接将参数传递给方法?是的,我知道performSelector不可能做到这一点。有关详细信息,请参阅。如果你真的需要多个参数,也许你可以尝试分派。我同意你的答案,在动态编码的同时,在传递参数的过程中,最好有一个字典。感谢onevcatFor谁需要它这里有一个示例:,它还解释了如何获取返回值。对于谁需要它,这里有一个示例:,它还解释了如何获取返回值。