2013-07-19 83 views
0

我想更新字典中不是可變的字典中'距離'的值,我有一個由json讀取的字典組成的字典,我需要更新應用程序中的所有距離值,你能否給我一個示例代碼,我該怎麼做?更新字典中的鍵的值

這裏是我創建這個數組:

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"; 
} 
) 

測試我試過,但即使我不能這樣簡單的字典更新值:

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:終止叫拋出異常

+1

你正在初始化一個不可變的NSMutableDictionary * dict。這個應該有效: NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: @「value1」,@「key1」,@「value2」,@「key2」,nil]; [字典setValue:@「test」forKey:@「key2」]; – Silenteh

回答

2

如果要修改JSONObjectWithData返回的字典,請使用NSJSONReadingMutableContainers選項。這會導致該方法返回可變容器類。例如:

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

我讀了'NSJSONReadingMutableContainers',但我不知道如何使用它?你可以請示例代碼提供給我嗎? – John

+0

查看更新後的答案。只需將它添加爲您對JSONObjectWithData調用的選項即可。 –

+0

它似乎有效,但我有一個問題,我的距離值是一個整數,但是當我想要更新它時,我有一個錯誤,說'Intmpicit轉換爲id不允許使用ARC'。 我該如何解決這個問題? [[self.seachResult objectAtIndex:0] setValue:23 forKey:@「distance」]; – John

0

NSDictionary是不可變的(創建後無法更改)。改爲查看NSMutableDictionary。順便說一句 - 很容易從NSDictionary創建一個NSMutableDictionary。

+0

可否請你提供一個示例代碼如何從NSDictionary創建NSMutableDictionary? – John

+0

NSMutableDictionary * mutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:dictionary]; –

+0

或[myImmutableDictionary mutableCopy]; –

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編輯器中輸入了代碼,所以我可能拼錯了一些。

+0

感謝您的代碼,self.seachResult是一個數組,我試圖修改您的代碼來工作,但因爲我是初學者無法做到這一點,你可以請更新代碼假設self.seachResult是一個數組不是字典? ! ,你寫了'dictionaryWithCapacity:10',但是項目的數量取決於查詢和json數據 – John

+0

[self.seachResult setObject:YOUR_UPDATED_VALUE_HERE forKey:@「distance」];不工作,我更新的問題,你可以請檢查嗎? – John

+0

@約翰對不起,我沒注意到self.searchResult是一個數組。 如果需要,NSDictionary會自動增加它的大小,即使認爲在手機上總是試圖用預期的大小初始化它,現在你提到self.searchResult是一個最好的數組,它初始化它像這樣: self。 seachResult = [NSMutableDictionary dictionaryWithCapacity:[self.seachResult count]]; 我會根據您的意見編輯我的答案。 – Silenteh