我想更新字段中的密钥'distance'的值不是Mutable,我有一个由json读取的字典数组,我需要更新app中的所有距离值,请你提供给我一个示例代码,我该怎么做?
这是我创建这个数组的地方:
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
self.seachResult =[NSJSONSerialization JSONObjectWithData:responseData options:nil error:nil];
以下是样本信息:
2013-07-20 00:04:41.753 MyApp[61075:16a03] sorted array of dictionaries: (
{
cid = 2;
distance = 0;
image = "http://images.png";
latitude = "48.245565";
longitude = "16.342333";
manual = "";
movie = "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v";
pcode = 023942435228;
pid = 1;
pname = "example product";
price = "12.00";
qrcode = "";
rid = 1;
rname = "Example Retailer Name";
sale = 0;
"sale_percent" = 0;
"sale_price" = "0.00";
text = "here is text about sample product number 1...\nasdasdasda\nsdfsdfsd\nSdfsdf
},
{
cid = 1;
distance = 0;
image = "";
latitude = "";
longitude = "";
manual = "";
movie = "";
pcode = 1;
pid = 3;
pname = "test product";
price = "46.00";
qrcode = "";
rid = 2;
rname = "";
sale = 0;
"sale_percent" = 0;
"sale_price" = "35.00";
text = "some text here...
},
{
cid = 2;
distance = 0;
image = "httpestImage";
latitude = "48.245565";
longitude = "16.342333";
manual = "";
movie = "";
pcode = 1;
pid = 2;
pname = "sample product 2";
price = "126.00";
qrcode = "";
rid = 1;
rname = "Example Retailer Name";
sale = 1;
"sale_percent" = 20;
"sale_price" = "99.99";
text = "here is text about sample product number 2...\nblah blah blah\nasdasdasd\nASdasdas\nASdasdasd";
}
)
for test我试过这个,但即使我不能更新这样一个简单字典的值:
NSMutableDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"value1", @"key1", @"value2", @"key2", nil];
[dict setValue:@"test" forKey:@"key2"];
错误:
2013-07-20 00:46:08.086 NSdictionaryTest [61130:11303] *由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[< __ NSDictionaryI 0x7556390> setValue:forUndefinedKey:]:此类不是密钥值2的密钥值编码。 * 第一次抛出调用堆栈: (0x1c90012 0x10cde7e 0x1d18fb1 0xb79e41 0xafb5f8 0xafb0e7 0x2200 0xf31c7 0xf3232 0x423d5 0x4276f 0x42905 0x4b917 0xf96c 0x1094b 0x21cb5 0x22beb 0x14698 0x1bebdf9 0x1bebad0 0x1c05bf5 0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x1017a 0x11ffc 0x1e5d 0x1d85) libc ++ abi.dylib:terminate调用抛出异常
答案 0 :(得分:2)
如果要修改JSONObjectWithData返回的字典,请使用NSJSONReadingMutableContainers选项。这会导致该方法返回可变容器类。例如:
self.seachResult =[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
答案 1 :(得分:2)
您是否需要使用不可变字典?
您可以使用NSMutableDictionary
我猜self.seachResult是你的NSDictionary,你可以简单地将它声明为NSMutableDictionary。
用以下内容初始化:
self.seachResult = [NSMutableDictionary dictionaryWithCapacity:10];
[self.seachResult addEntriesFromDictionary: NSJSONSerialization JSONObjectWithData:responseData options:nil error:nil]];
[self.seachResult setObject:YOUR_UPDATED_VALUE_HERE forKey:@"distance"];
另外你也可以创建一个新的NSMutableDictionary,并用非可变的条目填充它,如下所示:
NSMutableDictionary *mutableSearchResults = [NSMutableDictionary dictionaryWithCapacity:[self.seachResult count]];
[mutableSearchResults addEntriesFromDictionary: self.seachResult];
[self.seachResult setObject:YOUR_UPDATED_VALUE_HERE forKey:@"distance"];
如果你想再次返回一个不可变的,你可以这样做:
[mutableSearchResults copy];
我希望它有所帮助。 请注意我刚刚在SO编辑器中输入了代码,所以我可能拼错了一些。
答案 2 :(得分:0)
NSDictionary是不可变的(创建后无法更改)。请看一下NSMutableDictionary。 BTW - 从NSDictionary创建NSMutableDictionary很容易。