2

In Objective-C how should I best approximate what in Java I am doing like this:

static private String[] array {"A", "B", "C"};

What I think I need is a simple array that I can index into with some integers. Alternative suggestions are welcome but bear in mind that if I am getting stuck on this then I am pretty much hopeless anyway.

As a test, I have tried using

NSArray *array = [[NSArray alloc] initWithObjects:@"A", @"B", @"C"];

in the main method but any more than one array of this type and I get Sig 11 or 10 errors. This happens even if I just have the arrays followed by NSLog statements. Just one array only.

Is it the case that this type of array is just unworkable in the main method? I really don't understand why it causes errors when I add a second array. They aren't even large.

Johan B
  • 890
  • 3
  • 23
  • 39
pie
  • 33
  • 1
  • 1
  • 4

1 Answers1

8

The parameters to initWithObjects need to end in nil, like so:

NSArray *array = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", nil];
dstnbrkr
  • 4,305
  • 22
  • 23
  • This is mentioned quite explicitly in the documentation for the method, by the way. It's always good to check the docs for a method you're using, especially if it doesn't seem to be working right. – Chuck Apr 14 '09 at 02:37
  • For what it's worth, I just got into iPhone development and also had a lot of issues with initWithObjects. Now I know why. I didn't put the nil. – FogleBird Apr 14 '09 at 13:32
  • Indeed, you are correct. This has solved my problem and will teach me to check the documentation more thoroughly next time. Thanks. – pie Apr 14 '09 at 16:54