objective -c如何保存变量名称和值

时间:2012-03-21 10:42:38

标签: iphone objective-c ios cocoa

我有五个整数。我将所有整数添加到mutableArray并将其洗牌。

int A = 1;
int B = 2;
int C = 3;
int D = 4;
int E = 5;

myArray = [2,1,3,5,4]; //shuffled array

是否可以获取数组中每个整数的变量名? 请帮帮我。

3 个答案:

答案 0 :(得分:6)

在这种情况下,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
    } 

答案 1 :(得分:1)

如果我想这样做(并且跟踪每个变量在洗牌后的位置(这就是你想要做的,对吧?)),我会为每个条目创建一个NSDictionary,然后将其洗牌......

int aInt = 1;
int bInt = 2;
int cInt = 3;
int dInt = 4;
int eInt = 5;

NSDictionary* a = [NSDictionary dictionaryWithObjectsAndKeys:
                                                [NSNumber numberWithInt:aInt],@"value",
                                                "A",@"name"];

NSDictionary* b = [NSDictionary dictionaryWithObjectsAndKeys:
                                                [NSNumber numberWithInt:bInt],@"value",
                                                "B",@"name"];

NSDictionary* c = [NSDictionary dictionaryWithObjectsAndKeys:
                                                [NSNumber numberWithInt:cInt],@"value",
                                                "C",@"name"];

NSDictionary* d = [NSDictionary dictionaryWithObjectsAndKeys:
                                                [NSNumber numberWithInt:dInt],@"value",
                                                "D",@"name"];

NSDictionary* e = [NSDictionary dictionaryWithObjectsAndKeys:
                                                [NSNumber numberWithInt:eInt],@"value",
                                                "e",@"name"];

NSMutableArray* myArray = [[NSMutableArray alloc] initWithObjects:a,b,c,d,e,nil];

// Then... you may shuffle it...

答案 2 :(得分:-1)

更优雅的解决方案(没有5个不同的变量......但是int变量存储在数组中)

代码:

int num[10] = {1,2,3,4,5,6,7,8,9,10};
NSMutableArray* arr = [[NSMutableArray alloc] initWithObjects: nil];

for (int i=0; i<10; i++)
{
    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:num[i]],@"value",
                                                               [NSNumber numberWithInt:i],@"id",
                     nil];

    [arr addObject:dic];
}

NSLog(@"arr : %@",arr);

输出

arr : (
        {
        id = 0;
        value = 1;
    },
        {
        id = 1;
        value = 2;
    },
        {
        id = 2;
        value = 3;
    },
        {
        id = 3;
        value = 4;
    },
        {
        id = 4;
        value = 5;
    },
        {
        id = 5;
        value = 6;
    },
        {
        id = 6;
        value = 7;
    },
        {
        id = 7;
        value = 8;
    },
        {
        id = 8;
        value = 9;
    },
        {
        id = 9;
        value = 10;
    }
)