开发者

Dynamically setting NSString vars in a loop

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开发者_Python百科 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,


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜