What is this Objective-C syntax, ellipse style dot notation? “…”

前端 未结 1 962
青春惊慌失措
青春惊慌失措 2021-02-12 19:44

I noticed this in Joe Hewitt\'s source for Three20 and I\'ve never seen this particular syntax in Objective-C before. Not even sure how to reference it in an appropriate Google

1条回答
  •  一整个雨季
    2021-02-12 20:47

    It's a variadic method, meaning it takes a variable number of arguments. This page has a good demonstration of how to use it:

    #import 
    
    @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);
      }
    }
    

    0 讨论(0)
提交回复
热议问题