variable name from string in obj-c

后端 未结 2 1089
南方客
南方客 2020-12-07 05:04

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 d

相关标签:
2条回答
  • 2020-12-07 05:21

    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.

    0 讨论(0)
  • 2020-12-07 05:42

    Instead of having individual variables like this:

    int index1, index2, index3, ...indexN:
    

    you should consider using an array of indices:

    int index[N];
    

    and then you can sum in a loop, e.g.

    sum = 0;
    for (i = 0; i < N; ++i)
    {
        sum += array[index[i]];
    }
    
    0 讨论(0)
提交回复
热议问题