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 this in a loop by getting the current variable name from the loop index. How can i do this?
i = array[index1] + array[index2] + ... + array[indexn].
Instead of having separate variables:
int index1, index2, index3, ...indexN:
you should use an array of indices:
int index[N];
and then you can sum in a loop like
sum = 0; for (i = 0; i < N; ++i) { sum += array[index[i]]; }
Sorry, this is not possible in objective-c. It works for php for example.
There are ways to get objects by name if your data model allows it, but shared variable names cannot be synthesized by name.