如何将非终止字符串列表传递给Objective-C函数?

时间:2022-09-07 08:51:39

The function [NSArray arrayWithObjects:foo, bar, nil] passes a nil-terminted list of string to a function.

函数[NSArray arrayWithObjects:foo, bar, nil]将一个带nil端点的字符串列表传递给函数。

If I want to write a similar function, what does the declaration look like, and how do I iterate through the strings?

如果我想编写一个类似的函数,那么声明是什么样子的,我如何遍历字符串呢?

3 个解决方案

#1


9  

I quote http://developer.apple.com/mac/library/qa/qa2005/qa1405.html, which contains the full truth.

我引用了http://developer.apple.com/mac/library/qa/qa2005/qa1405.html,它包含了全部的真理。

Here's an example of an Objective-C category, containing a variadic method that appends all the objects in a nil-terminated list of arguments to an NSMutableArray instance:

这里有一个Objective-C类别的例子,它包含一个变量方法,它将nil终止的参数列表中的所有对象附加到一个NSMutableArray实例中:

#import <Cocoa/Cocoa.h>

@interface NSMutableArray (variadicMethodExample)

- (void) appendObjects:(id) firstObject, ...;  // This method takes a nil-terminated list of objects.

@end

@implementation NSMutableArray (variadicMethodExample)

- (void) appendObjects:(id) firstObject, ...
{
id eachObject;
va_list argumentList;
if (firstObject)                      // The first argument isn't part of the varargs list,
  {                                   // so we'll handle it separately.
  [self addObject: firstObject];
  va_start(argumentList, firstObject);          // Start scanning for arguments after firstObject.
  while (eachObject = va_arg(argumentList, id)) // As many times as we can get an argument of type "id"
    [self addObject: eachObject];               // that isn't nil, add it to self's contents.
  va_end(argumentList);
  }
}

@end

#2


0  

I'm not sure if Obj-c has its own particular support for this, but in C you do:

我不确定object - C是否有它自己的特别支持,但是在C中你有:

function nArgs(int n, ...)

To support multiple args, and the following style of implementation:

支持多种args,实现方式如下:

 {
        va_list ap;
        va_start(ap, n);

        while (n-- > 0) {
            int i = va_arg(ap, int);
        }

        va_end(ap); 
    }

Here for more info: http://www.cprogramming.com/tutorial/lesson17.html

这里有更多的信息:http://www.cprogramming.com/tutorial/lesson17.html。

#3


0  

NSArray declares that method like this:

NSArray这样宣布这个方法:

+ (id)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;

When you implement it, you'll need to use the va_arg macros to get at each argument.

实现它时,需要使用va_arg宏来获取每个参数。

Look up va_list, va_start(), va_arg(), etc.

查找va_list、va_start()、va_arg()等。

More info can be seen in the man page for va_arg and by looking at stdarg.h

在va_arg的手册页和查看stdarg.h可以看到更多信息

#1


9  

I quote http://developer.apple.com/mac/library/qa/qa2005/qa1405.html, which contains the full truth.

我引用了http://developer.apple.com/mac/library/qa/qa2005/qa1405.html,它包含了全部的真理。

Here's an example of an Objective-C category, containing a variadic method that appends all the objects in a nil-terminated list of arguments to an NSMutableArray instance:

这里有一个Objective-C类别的例子,它包含一个变量方法,它将nil终止的参数列表中的所有对象附加到一个NSMutableArray实例中:

#import <Cocoa/Cocoa.h>

@interface NSMutableArray (variadicMethodExample)

- (void) appendObjects:(id) firstObject, ...;  // This method takes a nil-terminated list of objects.

@end

@implementation NSMutableArray (variadicMethodExample)

- (void) appendObjects:(id) firstObject, ...
{
id eachObject;
va_list argumentList;
if (firstObject)                      // The first argument isn't part of the varargs list,
  {                                   // so we'll handle it separately.
  [self addObject: firstObject];
  va_start(argumentList, firstObject);          // Start scanning for arguments after firstObject.
  while (eachObject = va_arg(argumentList, id)) // As many times as we can get an argument of type "id"
    [self addObject: eachObject];               // that isn't nil, add it to self's contents.
  va_end(argumentList);
  }
}

@end

#2


0  

I'm not sure if Obj-c has its own particular support for this, but in C you do:

我不确定object - C是否有它自己的特别支持,但是在C中你有:

function nArgs(int n, ...)

To support multiple args, and the following style of implementation:

支持多种args,实现方式如下:

 {
        va_list ap;
        va_start(ap, n);

        while (n-- > 0) {
            int i = va_arg(ap, int);
        }

        va_end(ap); 
    }

Here for more info: http://www.cprogramming.com/tutorial/lesson17.html

这里有更多的信息:http://www.cprogramming.com/tutorial/lesson17.html。

#3


0  

NSArray declares that method like this:

NSArray这样宣布这个方法:

+ (id)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;

When you implement it, you'll need to use the va_arg macros to get at each argument.

实现它时,需要使用va_arg宏来获取每个参数。

Look up va_list, va_start(), va_arg(), etc.

查找va_list、va_start()、va_arg()等。

More info can be seen in the man page for va_arg and by looking at stdarg.h

在va_arg的手册页和查看stdarg.h可以看到更多信息