views:

28

answers:

1

I am building an iphone game. I have a set of NSStrings that I set periodically throughout the game life cycle. I am finding that I am in need of setting the same NSStrings often and looping through an array of NSStrings is ok for checking the value of the NSStrings atIndex:i, but setting them using the same method results in nothing. Basically I have a set of vars below that I'd like to set in a loop instead. Can someone show me how to re-write the following as a loop that will set the NSStrings properly?:

// reset state to first

square_01State = @"first";
square_02State = @"first";
square_03State = @"first";
square_04State = @"first";
square_05State = @"first";
square_06State = @"first";
square_07State = @"first";
square_08State = @"first";

square_09State = @"first";
square_10State = @"first";
square_11State = @"first";
square_12State = @"first";
square_13State = @"first";
square_14State = @"first";
square_15State = @"first";
square_16State = @"first";

There could potentially be hundreds of these, so I'd like to be able to set them all in a loop. Thanks,

+3  A: 

What you have is a Poor Man's Array. You should make it a proper array and set objects for the relevant indices. That will solve the problem as you've presented it.

If the situation is actually more complex than what you have presented here, the extra detail might help. As you have presented it here, this is a design problem consisting of having so many numbered variables instead of an array.

Chuck
So the array indexes should just hold @"first", instead of the actual NSString object? Duh!!! I see now. I am going around my elbow...Thanks - This is why I ask, 4 eyes are better than 2 :)
Damonmath