Hassy31
Hassy31

Reputation: 2813

objective-c how to save variable name and value

I have five integers.I added all integers to a mutableArray and shuffled it.

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

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

is it possible to get variable names of each integer in the array? please help me.

Upvotes: 0

Views: 1001

Answers (3)

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

A more... elegant solution (not with 5 different variables... but with int variables stored in an array)

Code :

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);

Output :

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

Upvotes: -1

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

If I wanted to do that (and keep track of where each variable is put after the shuffling (that's what you want to do, right?)), I would create an NSDictionary for each entry and then shuffle it...

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...

Upvotes: 1

Kanan Vora
Kanan Vora

Reputation: 2122

In this case NSDictionary would be feasible, you can store both the Variable Names and their Values:

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"A",@"2",@"B",@"3",@"C",@"4",@"D",@"5",@"E", nil];

Now you can fetch all the keys of the dictionary like:

NSArray *arr = [dict allKeys];      // which will be [A,B,C,D,E]

And you can fetch the values for those keys like:

for (int i=0; i<arr.count; i++) {
        NSLog(@"%@",[dict valueForKey:[arr objectAtIndex:i]]);  // Will print 1,2,3,4,5
    } 

Upvotes: 6

Related Questions