如何制作NSStrings数组?

时间:2016-10-07 01:24:14

标签: ios objective-c arrays nsstring

如果有人知道如何制作一个充满NSStrings的数组,我真的可以使用一些帮助,这无法在网上任何地方找到一个好的例子,这不是家庭工作,因为它没有到期,但它是一个我的老师给我研究的问题的pdf上的问题,似乎无法想象这一个。 所以,如果有人知道一个例子,一些解释会很好,谢谢你花时间在这上面。

1 个答案:

答案 0 :(得分:1)

目前还不完全清楚你在问什么。如果您只需要创建一个不可变数组,则可以使用对象文字语法:

 NSArray* stringArray = @[@"one", @"two", @"three"];

如果你想要一个可以修改的可变数组,你可以创建一个可变数组:

  //Create an empty mutable array
  NSMutableArray* stringArray = [NSMutableArray new];

  //Loop from 0 to 2.
  for (int index = 0; index < 3; index++) {
    //Create the strings "one", "two", and "three", one at a time
    NSString *aString = [formatter stringFromNumber: @(index+1)];

    //Add the new string at the next available index. Note that the
    //index must either an existing index into the array or the next available index.
    //if you try to index past the end of the array you will crash.
    stringArray[index] = aString;

    //You could use addObject instead:
    //[stringArray addObject: aString];
  }
  NSLog(@"%@", stringArray);