【问题标题】:how to make an array of NSStrings? [duplicate]如何制作一个NSStrings数组? [复制]
【发布时间】:2016-10-07 01:24:13
【问题描述】:

如果有人知道如何制作一个充满 NSStrings 的数组,我真的可以使用一些帮助来解决这个问题,在网上的任何地方都找不到一个很好的例子,这不是家庭作业,因为它不是到期的,但它是一个我的老师给我研究的问题的pdf上的问题,似乎无法弄清楚这个问题。 因此,如果有人知道带有一些解释的示例,那就太好了,感谢您抽出宝贵的时间。

【问题讨论】:

  • 对你来说最困难的部分是什么?

标签: ios objective-c arrays nsstring


【解决方案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);

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2013-05-28
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 2013-05-06
    相关资源
    最近更新 更多