And*_*son 3 iphone objective-c
函数[NSArray arrayWithObjects:foo,bar,nil]将一个nil-terminted字符串列表传递给一个函数.
如果我想编写一个类似的函数,声明是什么样的,我如何遍历字符串?
我引用http://developer.apple.com/mac/library/qa/qa2005/qa1405.html,其中包含完整的真相.
这是一个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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1127 次 |
| 最近记录: |