-2

Hi I have a method that gets a list of district and i need to display the district list in a pickerview. Here is the response that I am receiving:

[{"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"]

Here's what I have so far: (this called in textFieldDidBeginEditing)

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

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

when my code reaches [pickerView selectRow:0 inComponenet:0 animated:YES]; my app crashes. this is the error that i am getting:

[__NSCFDictionary length]: unrecognized selector sent to instance 0x1744784c0 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary length]: unrecognized selector sent to instance 0x1744784c0'

I am suspecting this is because of the nested array. How can I parse the jsonString that I am receiving. I've tried doing this:

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];

I'm still receiving the same error. Is there a way to parse the json response? Can I delete the values(Caloocan, Las Pinas, etc..) under @"NCR" key? When the calues are deleted, is "NCR" key still part of the original array (NCR, Abra, etc..)? Thanks!

1 Answers1

0

I assume you use the array "pickerComponentsArray" as Datasource for your PickerView. And that the array "districtArray" holds the json string.

Use this algorithm to clean up your 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];

I hope this helps. I just wrote it real quick, without testing.

You basically check the type of an object in the json Array. If it is a dictionary, you only use the keys of that dictionary. Otherwise you just use the object in the json.

dfinki
  • 324
  • 4
  • 13
  • hey, @dfinki thanks. the code works. one change though: NSDictionary *tmp = (NSDictionary *) mutableArray[i]; changed to: NSDictionary *tmp = (NSDictionary *) pickerComponentsArray[i]; – Arianne Lee Jul 28 '15 at 14:49
  • oh yeah, sry for the little mistake. I edited my answer real quick. Thanks for acceppting! :) – dfinki Jul 28 '15 at 15:06