2016-08-30 47 views
0

這裏是我的API響應 -的NSDictionary到NSMutableArray的

{"kitchen":"3","bath":"5","lanundry":"0","dining":"0","bedroom":"0"} 

我在NSDictionary裝這個,我可以使用鍵像檢索值 -

NSDictionary *pdata = [NSJSONSerialization JSONObjectWithData: data options:NSJSONReadingMutableContainers error:nil];  
[pdata objectForKey:@"dining"] 

我要添加dictionary對象爲NSMutableArray但它會覆蓋最後一個數組的所有數組值,即使它添加了五條記錄。

itemsA=[[NSMutableArray alloc] init]; 


imgCount *img=[[imgCount alloc] init]; 

if([pdata objectForKey:@"kitchen"]) 
    { 

     [img setCount:[pdata objectForKey:@"kitchen"]]; 
     [img setDesc:@"Kitchen"]; 

     [itemsA addObject:img]; 
    } 
if([pdata objectForKey:@"bath"]) 
    { 
     [img setCount:[pdata objectForKey:@"bath"]]; 
     [img setDesc:@"Bathroom"]; 
     [itemsA addObject:img]; 
    } 
if([pdata objectForKey:@"lanundry"]) 
    { 
     [img setCount:[pdata objectForKey:@"lanundry"]]; 
     [img setDesc:@"Laundry"]; 
     [itemsA addObject:img]; 
     } 
if([pdata objectForKey:@"dining"]) 
     { 
     [img setCount:[pdata objectForKey:@"kitchen"]]; 
     [img setDesc:@"Kitchen"]; 
     [itemsA addObject:img]; 
     } 
    if([pdata objectForKey:@"bedroom"]) 
     { 
     [img setCount:[pdata objectForKey:@"bedroom"]]; 
     [img setDesc:@"Bedroom"]; 
     [itemsA addObject:img]; 
     } 

我的表視圖顯示5列的最後一個值 -

Bedroom (0) 

但我想造成這樣

{"desc":"bathroom","count":"0"}, {"desc":"dining","count":"3"} 
+0

結果數組應該怎麼樣? – ozgur

+0

你想在nsmutablearray中添加新的對象? –

+0

我想存儲爲{「desc」:「bathroom」,「count」:「0」},{「desc」:「dining」,「count」:「3」}等 – Tajuddin

回答

2

與您的代碼的問題是要創建只有一個img對象並重新使用它。由於所有對象均通過Objective-C中的引用進行訪問,因此您需要在全部條件下創建單獨的img對象。試試這個

itemsA=[[NSMutableArray alloc] init]; 

if([pdata objectForKey:@"kitchen"]) 
    { 
     imgCount *img=[[imgCount alloc] init]; 
     [img setCount:[pdata objectForKey:@"kitchen"]]; 
     [img setDesc:@"Kitchen"]; 

     [itemsA addObject:img]; 
    } 
if([pdata objectForKey:@"bath"]) 
    { 
     imgCount *img=[[imgCount alloc] init]; 
     [img setCount:[pdata objectForKey:@"bath"]]; 
     [img setDesc:@"Bathroom"]; 
     [itemsA addObject:img]; 
    } 
if([pdata objectForKey:@"lanundry"]) 
    { 
     imgCount *img=[[imgCount alloc] init]; 
     [img setCount:[pdata objectForKey:@"lanundry"]]; 
     [img setDesc:@"Laundry"]; 
     [itemsA addObject:img]; 
     } 
if([pdata objectForKey:@"dining"]) 
     { 
     imgCount *img=[[imgCount alloc] init]; 
     [img setCount:[pdata objectForKey:@"kitchen"]]; 
     [img setDesc:@"Kitchen"]; 
     [itemsA addObject:img]; 
     } 
    if([pdata objectForKey:@"bedroom"]) 
     { 
     imgCount *img=[[imgCount alloc] init]; 
     [img setCount:[pdata objectForKey:@"bedroom"]]; 
     [img setDesc:@"Bedroom"]; 
     [itemsA addObject:img]; 
     } 
+0

是的,就是這樣。你是冠軍。我沒有意識到我必須在每個塊中初始化。非常感謝你。 – Tajuddin

+0

Upvoted爲這個答案,因爲你給起始者可以理解的答案 – user3182143

+0

@Tajuddin我已經創建了字典,因爲在問題評論你把你的反應就像你想要的字典數組,我編輯了我的答案,請檢查一次,它會幫助你。 –

0

試試這個,讓你的所有字典鍵進入NSMutable陣列。

NSArray*keys=[dict allKeys]; 
NSMutableArray *mutableArray = [keys mutableCopy]; 
+0

這是返回 - 'code' 2016-08-30 14:57:31.303 ACF [23914:9204243] Copy array - ( bath, lanundry, 餐飲, 臥室,廚房 ) – Tajuddin

7

因此,您可以嘗試這樣。首先獲取字典的所有關鍵字,然後使用該關鍵字數組創建字典數組。

NSArray*keys=[dict allKeys]; 
NSMutableArray *array = [[NSMutableArray alloc] init]; 
for (NSString *key in keys) { 
    NSDictionary *dic = @{ @"desc":key, @"count": [dict objectForKey:key] }; 
    [array addObject:dic]; 
} 
NSLog(@"Array - %@",array); 

編輯:如果你想自定義類對象,你可以嘗試這樣的。

for (NSString *key in keys) { 
    imgCount *imgObject=[[imgCount alloc] init]; 
    [imgObject setCount:[pdata objectForKey:key]]; 
    [imgObject setDesc:[key capitalizedString]];   
    [array addObject:imgObject]; 
} 
+0

這是一個更好的解決方案,因爲它使用通用的方式來訪問字典元素。即使我這樣建議:) – Arun

+0

謝謝,在這種情況下,我必須修改單元格中的密鑰,因爲我想顯示適當的initcap詞,如「浴室」,因爲密鑰有「浴室」。 – Tajuddin

+0

@Tajuddin你有沒有嘗試使用'capitalizedString'作爲我編輯的答案? –

相關問題