views:

242

answers:

1

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 then 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.

+8  A: 

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

NSArray *array = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", nil];
dbarker
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
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
Indeed, you are correct. This has solved my problem and will teach me to check the documentation more thoroughly next time. Thanks.
pie