【问题标题】:What is this Objective-C syntax, ellipse style dot notation? "..."这是什么 Objective-C 语法,椭圆样式的点表示法? “……”
【发布时间】:2009-04-02 14:53:39
【问题描述】:

我在 Joe Hewitt 的 Three20 源代码中注意到了这一点,而且我以前从未在 Objective-C 中看到过这种特殊语法。甚至不知道如何在适当的 Google 搜索中引用它。

来自 TTTableViewDataSource:

+ (TTSectionedDataSource*)dataSourceWithObjects:(id)object,... {

“...”是让我失望的地方。我假设它是一种枚举形式,其中可以提供可变数量的参数。如果是,该运算符的正式名称是什么,我在哪里可以参考它的文档?

谢谢。

【问题讨论】:

    标签: objective-c cocoa syntax


    【解决方案1】:

    这是一种可变参数方法,这意味着它需要可变数量的参数。 This page 很好地演示了如何使用它:

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

    【讨论】:

    • 哈哈,比我还快,同一个链接!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    相关资源
    最近更新 更多