-4

If anyone knows how to make an array filled with NSStrings i could really use some help with this can't find a good example of this anywhere on the net, this isn't home work because its not due but it is a question that is on a pdf of questions my teacher gave me to study, just can't seem to figure this one out. so if anyone knows an example with some explanation would be great, thanks for taking your time on this.

1 Answers1

1

It isn't completely clear what you are asking. If you just need to create an immutable array, you can use object literal syntax:

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

If you want a mutable array that you can modify, you can create a mutable array:

  //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);
Duncan C
  • 128,072
  • 22
  • 173
  • 272