0

辞書を含む配列 self.seachResult があります。すべての辞書に項目を追加したいです。このようなコードを試しましたが、エラーが発生しました。項目「距離」を追加するコードを教えてください。

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

1 に答える 1

3

から作成されたオブジェクトJSONはすべて不変です。

self.seachResult =[NSJSONSerialization JSONObjectWithData:responseData options:nil error:nil];

passnilを渡す代わりに:optionsNSJSONReadingMutableContainers

self.seachResult =[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];

NSJSONSerializationドキュメントから:

NSJSONReadingMutableContainers

配列と辞書が変更可能なオブジェクトとして作成されることを指定します。

于 2013-07-19T01:28:37.667 に答える