我有五个整数。我将所有整数添加到一个 mutableArray 中并将其打乱。
int A = 1;
int B = 2;
int C = 3;
int D = 4;
int E = 5;
myArray = [2,1,3,5,4]; //shuffled array
是否可以获取数组中每个整数的变量名? 请帮助我。
最佳答案
在这种情况下 NSDictionary 是可行的,您可以存储变量名称及其值:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"A",@"2",@"B",@"3",@"C",@"4",@"D",@"5",@"E", nil];
现在你可以像这样获取字典的所有键:
NSArray *arr = [dict allKeys]; // which will be [A,B,C,D,E]
您可以像这样获取这些键的值:
for (int i=0; i<arr.count; i++) {
NSLog(@"%@",[dict valueForKey:[arr objectAtIndex:i]]); // Will print 1,2,3,4,5
}
关于iphone - objective-c 如何保存变量名和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9802932/