Objective C 实现带有参数数组的方法

发布于 2024-10-21 17:10:01 字数 454 浏览 1 评论 0原文

Hee

有谁知道如何在 Objective C 中实现一个方法,该方法将采用一组参数作为参数,例如:

[NSArray arrayWithObjects:@"A",@"B",nil];

该方法的方法声明是:

+ (id)arrayWithObjects:(id)firstObj...

我似乎无法自己制作这样的方法。我做了以下操作:

+ (void) doSometing:(id)string manyTimes:(NSInteger)numberOfTimes;

[SomeClass doSometing:@"A",@"B",nil manyTimes:2];

它将给出警告函数“doSometing:manyTimes:”的参数太多,

已经谢谢了。

Hee

Does anybody know how to implement an method in objective c that will take an array of arguments as parameter such as:

[NSArray arrayWithObjects:@"A",@"B",nil];

The method declaration for this method is:

+ (id)arrayWithObjects:(id)firstObj...

I can't seem to make such method on my own. I did the following:

+ (void) doSometing:(id)string manyTimes:(NSInteger)numberOfTimes;

[SomeClass doSometing:@"A",@"B",nil manyTimes:2];

It will give the warningtoo many arguments to function 'doSometing:manyTimes:'

Thanks already.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬花如无物 2024-10-28 17:10:01

省略号(...)继承自C;您只能将其用作调用中的最后一个参数(并且您在示例中错过了相关的逗号)。因此,在您的情况下,您可能会想要:

+ (void)doSomethingToObjects:(id)firstObject, ...;

或者,如果您希望计数是明确的并且可以想出一种很好的措辞方式:

+ (void)doManyTimes:(NSInteger)numberOfTimes somethingToObjects:(id)firstObject, ...;

然后您可以使用普通的 C 方法来处理省略号,该方法位于 stdarg.h 中。 这里有一个快速文档,示例用法是:

+ (void)doSomethingToObjects:(id)firstObject, ...
{
    id object;
    va_list argumentList;

    va_start(argumentList, firstObject);
    object = firstObject;

    while(1)
    {
        if(!object) break; // we're using 'nil' as a list terminator

        [self doSomethingToObject:object];
        object = va_arg(argumentList, id);
    }

    va_end(argumentList);
}

编辑:补充,回应评论。由于 C 处理函数调用的方式(由 Objective-C 继承,尽管不是很明显),您无法将省略号中传递给您的各种内容传递给另一个采用省略号的函数。相反,您倾向于传递 va_list。例如

+ (NSString *)doThis:(SEL)selector makeStringOfThat:(NSString *)format, ...
{
    // do this
    [self performSelector:selector];

    // make string of that...

    // get the argument list
    va_list argumentList;
    va_start(argumentList, format);

    // pass it verbatim to a suitable method provided by NSString
    NSString *string = [[NSString alloc] initWithFormat:format arguments:argumentList];

    // clean up
    va_end(argumentList);

    // and return, as per the synthetic example
    return [string autorelease];
}

The ellipsis (...) is inherited from C; you can use it only as the final argument in a call (and you've missed out the relevant comma in your example). So in your case you'd probably want:

+ (void)doSomethingToObjects:(id)firstObject, ...;

or, if you want the count to be explicit and can think of a way of phrasing it well:

+ (void)doManyTimes:(NSInteger)numberOfTimes somethingToObjects:(id)firstObject, ...;

You can then use the normal C methods for dealing with ellipses, which reside in stdarg.h. There's a quick documentation of those here, example usage would be:

+ (void)doSomethingToObjects:(id)firstObject, ...
{
    id object;
    va_list argumentList;

    va_start(argumentList, firstObject);
    object = firstObject;

    while(1)
    {
        if(!object) break; // we're using 'nil' as a list terminator

        [self doSomethingToObject:object];
        object = va_arg(argumentList, id);
    }

    va_end(argumentList);
}

EDIT: additions, in response to comments. You can't pass the various things handed to you in an ellipsis to another function that takes an ellipsis due to the way that C handles function calling (which is inherited by Objective-C, albeit not obviously so). Instead you tend to pass the va_list. E.g.

+ (NSString *)doThis:(SEL)selector makeStringOfThat:(NSString *)format, ...
{
    // do this
    [self performSelector:selector];

    // make string of that...

    // get the argument list
    va_list argumentList;
    va_start(argumentList, format);

    // pass it verbatim to a suitable method provided by NSString
    NSString *string = [[NSString alloc] initWithFormat:format arguments:argumentList];

    // clean up
    va_end(argumentList);

    // and return, as per the synthetic example
    return [string autorelease];
}
Saygoodbye 2024-10-28 17:10:01

多个参数(也称为 arglist)只能出现在方法声明的末尾。您的 doSomething 方法将如下所示:

+ (void)doNumberOfTimes:(NSInteger)numberOfTimes withStrings:(id)firstArg, ...
{
    va_list args;
    va_start(args, firstArg);

    NSString * argString = firstArg;
    while (argString != nil)
    {
        // do something with argString here

        argString = va_arg(args, NSString *);
    }

    va_end(args);
}

调用方式如下:

[SomeClass doNumberOfTimes:2 withStrings:@"A", @"B", nil];

另请参阅:如何在 Objective-C 中创建可变参数方法

Multiple arguments (also known as an arglist) can only come at the end of a method declaration. Your doSomething method would look something like this:

+ (void)doNumberOfTimes:(NSInteger)numberOfTimes withStrings:(id)firstArg, ...
{
    va_list args;
    va_start(args, firstArg);

    NSString * argString = firstArg;
    while (argString != nil)
    {
        // do something with argString here

        argString = va_arg(args, NSString *);
    }

    va_end(args);
}

To be called as follows:

[SomeClass doNumberOfTimes:2 withStrings:@"A", @"B", nil];

See also: How to create variable argument methods in Objective-C

遇到 2024-10-28 17:10:01

我认为您正在追求可变参数函数。这是苹果的文档: http://developer.apple.com/library/mac /qa/qa2005/qa1405.html

I think you're after a variadic function. Here's Apple's documentation: http://developer.apple.com/library/mac/qa/qa2005/qa1405.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文