Objective-c:访问方法中的可变参数[duplicate]

时间:2023-01-15 14:58:37

Possible Duplicate:
How to create variable argument methods in Objective-C
Variable number of method parameters in Objective C - Need an example

可能重复:如何在Objective-C中创建变量参数方法-C中变量参数的数量-需要一个例子

Following is an example of a method having variadic arguments.

下面是一个具有可变参数的方法示例。

- (void)numberOfParameters:group,... {
    NSLog(@"%@",group);
}

In above method, I know to access the first one of the variadic arguments. Would you please help me for accessing the others as well?

在上面的方法中,我知道要访问第一个变量参数。你能帮我把其他的也联系上吗?

I am just going through ObjC.pdf & I am reading page number 35 & line number is 4

我正在浏览ObjC。我正在阅读第35页,第4行

2 个解决方案

#1


6  

See this almost same question

看看这个几乎相同的问题

-(void)yourMethods:(id)string1, ...{

    NSMutableArray *arguments=[[NSMutableArray alloc]initWithArray:nil];
    id eachObject;
    va_list argumentList;
    if (string1) 
    {
        [arguments addObject: string1];
        va_start(argumentList, string1); 
        while ((eachObject = va_arg(argumentList, id)))    
        {
             [arguments addObject: eachObject];
        }
        va_end(argumentList);        
     }
    NSLog(@"%@",arguments);
}

Call it with nil parameter at the end as:

以nil参数调用它,如:

[object yourMethods:arg1,arg2,arg3,nil];// object can be self

#2


3  

One: they're not called "group parameters" (as far as I know), but rather variadic arguments.

第一:它们不是“组参数”(据我所知),而是可变参数。

Two: the C standard library header stdarg.h provides data types and macros for this purpose (that's why I generally suggest to master plain ol' ANSI C first, before making The Best iPhone App Ever (TM)...)

二:C标准库头stdarg。h提供了用于此目的的数据类型和宏(这就是为什么我通常建议在做出有史以来最好的iPhone应用(TM)之前,先掌握简单的ol' ANSI C…)

#include <stdarg.h>

- (void)numberOfParameters:(int)num, ...
{
    int i;
    va_list args;
    va_start(args, num);

    for (i = 0; i < num; i++) {
        SomeType param = va_arg(args, SomeType);
        // do something with `param'
    }

    va_end(args);
}

Here's a rather good explanation on this topic.

关于这个话题有一个很好的解释。

#1


6  

See this almost same question

看看这个几乎相同的问题

-(void)yourMethods:(id)string1, ...{

    NSMutableArray *arguments=[[NSMutableArray alloc]initWithArray:nil];
    id eachObject;
    va_list argumentList;
    if (string1) 
    {
        [arguments addObject: string1];
        va_start(argumentList, string1); 
        while ((eachObject = va_arg(argumentList, id)))    
        {
             [arguments addObject: eachObject];
        }
        va_end(argumentList);        
     }
    NSLog(@"%@",arguments);
}

Call it with nil parameter at the end as:

以nil参数调用它,如:

[object yourMethods:arg1,arg2,arg3,nil];// object can be self

#2


3  

One: they're not called "group parameters" (as far as I know), but rather variadic arguments.

第一:它们不是“组参数”(据我所知),而是可变参数。

Two: the C standard library header stdarg.h provides data types and macros for this purpose (that's why I generally suggest to master plain ol' ANSI C first, before making The Best iPhone App Ever (TM)...)

二:C标准库头stdarg。h提供了用于此目的的数据类型和宏(这就是为什么我通常建议在做出有史以来最好的iPhone应用(TM)之前,先掌握简单的ol' ANSI C…)

#include <stdarg.h>

- (void)numberOfParameters:(int)num, ...
{
    int i;
    va_list args;
    va_start(args, num);

    for (i = 0; i < num; i++) {
        SomeType param = va_arg(args, SomeType);
        // do something with `param'
    }

    va_end(args);
}

Here's a rather good explanation on this topic.

关于这个话题有一个很好的解释。