Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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
如何在Objective-C中切换NSDictionary中的两个值?_Objective C_Xcode_Cocoa Touch - Fatal编程技术网

如何在Objective-C中切换NSDictionary中的两个值?

如何在Objective-C中切换NSDictionary中的两个值?,objective-c,xcode,cocoa-touch,Objective C,Xcode,Cocoa Touch,我有一个带有两个值的NSDictionary。现在我想切换这些值。这是我的密码: NSDictionary *aDictionary = [[NSDictionary alloc] init]; [aDictionary setValue:@"theValue1" forKey:@"theKey1"]; [aDictionary setValue:@"theValue2" forKey:@"theKey2"]; NSString *tmpValue = [aDictionary objectF

我有一个带有两个值的
NSDictionary
。现在我想切换这些值。这是我的密码:

NSDictionary *aDictionary = [[NSDictionary alloc] init];
[aDictionary setValue:@"theValue1" forKey:@"theKey1"];
[aDictionary setValue:@"theValue2" forKey:@"theKey2"];

NSString *tmpValue = [aDictionary objectForKey:@"theKey1"];
[aDictionary setValue:[myDict objectForKey:@"theKey2"] forKey:@"theKey1"];
[aDictionary setValue:tmpValue forKey:[myDict objectForKey:@"theKey2"]];
NSLog(@"%@", myDict); 


//output
theKey1 = theValue2;
theKey2 = theValue2;
theValue2 = theValue1;

我做错了什么?

除了几个更大的问题(如果要更改内容,应该使用
NSMutableDictionary
而不是
NSDictionary
,并且应该使用
setObject:forKey:
方法而不是
setValue:forKey:
方法),问题的根源是这一行:

[aDictionary setValue:tmpValue forKey:[myDict objectForKey:@"theKey2"]];

想想你在设置什么键。

明白了,伙计。非常感谢。我一定是盯着屏幕看太久了