iOS 多参数方法实现

来源:互联网 发布:网络编辑怎么做 编辑:程序博客网 时间:2024/06/08 10:36
  1. @interface NSMutableArray (variadicMethodExample)  
  2.   
  3. - (void) appendObjects:(id) firstObject, ...; // This method takes a nil-terminated list of objects.  
  4.   
  5. @end  
  6.   
  7. @implementation NSMutableArray (variadicMethodExample)  
  8.   
  9. - (void) appendObjects:(id) firstObject, ...  
  10. {  
  11. id eachObject;  
  12. va_list argumentList;  
  13. if (firstObject) // The first argument isn't part of the varargs list,  
  14.   {                                   // so we'll handle it separately.  
  15.   [self addObject: firstObject];  
  16.   va_start(argumentList, firstObject); // Start scanning for arguments after firstObject.  
  17.   while (eachObject = va_arg(argumentList, id)) // As many times as we can get an argument of type "id"  
  18.       [self addObject: eachObject]; // that isn't nil, add it to self's contents.  
  19.   va_end(argumentList);  
  20.   }  
  21. }  
0 0