代码之家  ›  专栏  ›  技术社区  ›  itsaboutcode

Objective-C中数组初始化结束时“nil”的用途

  •  1
  • itsaboutcode  · 技术社区  · 14 年前

    我已经看到并完成了数组的初始化,并且在初始化的末尾都加上了“nil”,但是我从来没有问过,为什么要把它放在那里?

    另外,如果在循环中初始化数组,是否仍然需要将nil放在数组的末尾?例如。

    array = [[NSMutableArray alloc] init];
    
    for (int i = 0 ; i < 10; i++)
    {
       [array addObject:@"1"];
    }
    
    // now this line is required or not after i exit the loop?
    [array addObject:nil];
    
    1 回复  |  直到 14 年前
        1
  •  6
  •   Jacob Relkin    14 年前

    这个概念叫做 无终止 哨兵