我有一个包含字典的数组self.seachResult,我想在所有字典中添加一个项目我尝试了这样的代码但是我收到了错误,你可以通过添加项目“距离”的代码向我提供< / p>
[[self.seachResult objectAtIndex:0] setObject:@"distance" forKey:@"400"];
这是我的字典数组:
2013-07-19 02:13:24.929 MyApp[59321:16a03] sorted array of dictionaries: (
{
cid = 2;
image = "http:///images/loginlogo.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\nSDfsdfs\ndfsdfsdf\n\n\n";
},
{
cid = 1;
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;
image = "http:///testImage.png";
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";
}
)
这是错误:
2013-07-19 02:49:54.261 MyApp[59376:16a03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
*** First throw call stack:
(0x3367012 0x2d27e7e 0x3366deb 0x332d347 0x13472 0x1d4d1c7 0x1d4d232 0x1d4d4da 0x1d648e5 0x1d649cb 0x1d64c76 0x1d64d71 0x1d6589b 0x1d65e93 0x1d65a88 0x20c1e63 0x20b3b99 0x1d4ddd2 0x12b9f 0x2d3b705 0x1c6f2c0 0x1c6f258 0x1d30021 0x1d3057f 0x1d2f6e8 0x1c9ecef 0x1c9ef02 0x1c7cd4a 0x1c6e698 0x37a7df9 0x37a7ad0 0x32dcbf5 0x32dc962 0x330dbb6 0x330cf44 0x330ce1b 0x37a67e3 0x37a6668 0x1c6bffc 0x288d 0x27b5)
libc++abi.dylib: terminate called throwing an exception
这是我创建它的地方:
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
self.seachResult =[NSJSONSerialization JSONObjectWithData:responseData options:nil error:nil];
// [self.productsTableView reloadData];
NSMutableArray *testSorted = [self.seachResult sortedArrayUsingComparator:^NSComparisonResult(NSMutableDictionary *obj1, NSMutableDictionary *obj2) {
NSString *price1 = obj1[@"price"];
NSString *price2 = obj2[@"price"];
NSNumber *n1 = [NSNumber numberWithFloat:[price1 floatValue]];
NSNumber *n2 = [NSNumber numberWithFloat:[price2 floatValue]];
return [n1 compare:n2];
}];
[[self.seachResult objectAtIndex:0] setObject:@"distance" forKey:@"400"];
NSLog(@"sorted array of dictionaries: %@", testSorted);
答案 0 :(得分:3)
从JSON
创建的对象都是不可变的。
self.seachResult =[NSJSONSerialization JSONObjectWithData:responseData options:nil error:nil];
而不是传递nil
options
传递NSJSONReadingMutableContainers
:
self.seachResult =[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
来自NSJSONSerialization文档:
NSJSONReadingMutableContainers
指定将数组和字典创建为可变对象。