来自obj-c中字符串的变量名称 [英] variable name from string in obj-c

查看:81
本文介绍了来自obj-c中字符串的变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆名为index1,index2,...,indexn的变量.我想计算i = array[index1] + array[index2] + ... + array[indexn].,我听说可以循环执行,从循环索引中获取当前变量名.我该怎么办?

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?

推荐答案

而不是像这样的单个变量:

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]];
}

这篇关于来自obj-c中字符串的变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆