2015-07-28 74 views
-2

嗨我有一個方法,獲取區的列表,我需要在pickerview中顯示區列表。 下面是我收到的迴應:如何通過值(非鍵)刪除數組中的項目

[{"NCR":[ "Caloocan", "Las Piñas", "Makati", "Malabon ", "Mandaluyong", "Manila", "Marikina", "Muntinlupa", "Navotas", "Parañaque", "Pasay", "Pasig", "Quezon", "San Juan", "Taguig", "Valenzuela", "Pateros"]}, "Abra", "Agusan del Norte", "Agusan del Sur", "Aklan", "Albay", "Antique", "Apayao", "Aurora", "Basilan", "Bataan", "Batanes", "Batangas", "Benguet", "Biliran", "Bohol", "Bukidnon", "Bulacan", "Cagayan", "Camarines Norte", "Camarines Sur", "Camiguin", "Capiz", "Catanduanes", "Cavite", "Cebu", "Compostela Valley", "Cotabato", "Davao del Norte", "Davao del Sur", "Davao Oriental", "Dinagat Islands", "Eastern Samar", "Guimaras", "Ifugao", "Ilocos Norte", "Ilocos Sur", "Iloilo", "Isabela", "Kalinga", "La Union", "Laguna", "Lanao del Norte", "Lanao del Sur", "Leyte", "Maguindanao", "Marinduque", "Masbate", "Metro Manila", "Misamis Occidental", "Misamis Oriental", "Mountain Province", "Negros Occidental", "Negros Oriental", "Northern Samar", "Nueva Ecija", "Nueva Vizcaya", "Occidental Mindoro", "Oriental Mindoro", "Palawan", "Pampanga", "Pangasinan", "Quezon", "Quirino"] 

這是我到目前爲止有:(這稱爲textFieldDidBeginEditing)

else if(textField == _districtTextField) 
{ 
    pickerComponentsArray = districtArray; 
    [pickerView reloadAllComponents]; 
    [pickerView selectRow:0 inComponent:0 animated:YES]; 

    [textField setText:[districtArray objectAtIndex:0]]; 
    [textField setInputView:pickerViewContainer]; 
} 

,當我的代碼已達到[pickerView selectRow:0 inComponenet:0動畫:是];我的應用程序崩潰。這是我收到的錯誤:

[__NSCFDictionary長度]:無法識別的選擇發送到實例0x1744784c0 終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,原因:「 - [__ NSCFDictionary長度]:無法識別的選擇發送到實例0x1744784c0'

我懷疑這是因爲嵌套數組。我如何解析我收到的jsonString。 我試着這樣做:

pickerComponentsArray = districtArray; 
NSMutableArray* mutableArray = [pickerComponentsArray mutableCopy]; 
for(int i=0;i<mutableArray.count;i++){ 
    NSMutableDictionary *outerDictionary = [mutableArray objectAtIndex:i]; 
    for(NSString *key in outerDictionary.allKeys){ 
     NSMutableDictionary *innerDictionary = [[outerDictionary objectForKey:key] mutableCopy]; 
     [innerDictionary removeObjectForKey:key]; 
    } 

     [pickerView reloadAllComponents]; 
     [pickerView selectRow:0 inComponent:0 animated:YES]; 

     [textField setText:[districtArray objectAtIndex:0]]; 
     [textField setInputView:pickerViewContainer]; 

我仍然收到了同樣的錯誤。有沒有辦法解析JSON響應?我可以刪除@「NCR」鍵下的值(Caloocan,Las Pinas等)嗎?當calues被刪除時,「NCR」鍵仍然是原始數組的一部分(NCR,Abra等)。 謝謝!

+0

將數組的所有鍵與它們的值進行比較並刪除鍵值對.. – iHart

+0

http://stackoverflow.com/questions/111866/best-way-to-remove-from-nsmutablearray-while-iterating勾選此項回答 – vijeesh

+0

你的回答是「陣列**的字典數組」。 – Sujay

回答

0

我假設你使用數組「pickerComponentsArray」作爲你的PickerView的數據源。 並且數組「districtArray」包含json字符串。

使用此算法來清理你的pickerComponentsArray:

NSArray *pickerComponentsArray = districtArray; 
NSMutableArray* mutableArray = [[NSMutableArray alloc]init]; 
for(int i = 0 ; i < pickerComponentsArray.count; i++){ 
    if ([[pickerComponentsArray[i] class] isSubclassOfClass:[NSDictionary class]]){ 
     NSDictionary *tmp = (NSDictionary *) pickerComponentsArray[i]; 
     [mutableArray addObjectsFromArray:[tmp allKeys]]; 
    } else { 
     [mutableArray addObject:pickerComponentsArray[i]]; 
    } 
} 
pickerComponentsArray = [mutableArray copy]; 

[pickerView reloadAllComponents]; 
[pickerView selectRow:0 inComponent:0 animated:YES]; 

[textField setText:[districtArray objectAtIndex:0]] 
[textField setInputView:pickerViewContainer]; 

我希望這有助於。我只是寫得很快,沒有測試。

你基本上檢查了json數組中的一個對象的類型。如果它是字典,則只能使用該字典的鍵。否則,您只需在json中使用該對象。

+0

嘿,@dfinki謝謝。代碼起作用。儘管一個變化:NSDictionary * tmp =(NSDictionary *)mutableArray [i];改爲:NSDictionary * tmp =(NSDictionary *)pickerComponentsArray [i]; –

+0

哦,是的,爲了小錯誤。我很快編輯了我的答案。 感謝您的支持! :) – dfinki

相關問題