Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone objective-c如何保存变量名称和值_Iphone_Objective C_Ios_Cocoa - Fatal编程技术网

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

Iphone objective-c如何保存变量名称和值,iphone,objective-c,ios,cocoa,Iphone,Objective C,Ios,Cocoa,我有五个整数。我将所有整数添加到一个可变数组中并将其洗牌 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

我有五个整数。我将所有整数添加到一个可变数组中并将其洗牌

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
    } 

for(int i=0;i在这种情况下,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
    } 
for(int i=0;i如果我想这样做(并跟踪洗牌后每个变量放在哪里(这就是你想做的,对吧?),我会为每个条目创建一个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...
如果我想这样做(并跟踪每个变量在洗牌后放置的位置(这就是您想要做的,对吗?),我会为每个条目创建一个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...

更优雅的解决方案(不是使用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);

更优雅的解决方案(不是使用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);

如果只使用数组,只需使用数组-->字典即可。我的意思是为每个变量设置字典,每个变量都有其值和变量名,可以在需要时为您提供名称和值。如果答案对您有用,请接受我的回答……如果只使用数组,只需使用数组-->字典即可。我的意思是设置字典每一个变量都有它的值和变量名,当你需要它时,它可以给你名字和值。如果它对你有用,请接受我的回答。。。