I have a bunch of variables named index1, index2, ..., indexn. I want to calculate i = array[index1] + array[index2] + ... + array[indexn]. I heard that I can do that in a loop, getting the current variable name from the loop index. How can I do that?
-
3Having a bunch of numbered variables is clue #1 that there's something wrong with your program's structure. Why aren't they in an array or other appropriate compound object?Nicholas Knight– Nicholas Knight2010-07-02 10:33:30 +00:00Commented Jul 2, 2010 at 10:33
Add a comment
|
2 Answers
Sorry, this is not possible in objective-c. It works for example in php.
There are ways to get objects by name if your data model allows for this, but in general variable names cannot be synthesized by name.
1 Comment
Eiko
This works only if you are using key value coding (KVC) to hold your variables. I think they are just plain ints in your code and not KVC compliant properties. May think about refactoring them into an array.