Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9087973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:49:09+00:00 2026-06-16T21:49:09+00:00

I have a pickerview with which I would want to display values using JSON

  • 0

I have a pickerview with which I would want to display values using JSON parsing. I am aware some questions on the site already tackle parsing and pickerview but mine is slightly different in regards to the structure of my json file. Here’s a sample of the json file.

[
{
"model":"juice",
"id" :
  [
    {
        "version": "version01"
    },

    {
        "version": "version02"
    }

  ]
},
 {
"model":"cream",
"id" :
[
  {
      "version": "cream01"
  },

  {
      "version": "cream02"
  }
]
}
]

and my .m file

- (void)viewDidLoad
    {
    [super viewDidLoad];

    NSURL * serverhost = [NSURL URLWithString:@"http://my.json"];

    NSError *error;

    NSData  * Data  = [NSData  dataWithContentsOfURL: serverhost];

    self.modelsArray= [NSJSONSerialization JSONObjectWithData:Data   options:NSJSONReadingAllowFragments error:&error];

    NSLog(@"%@", modelsArray);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    if (component == departments)
        return [self.modelsArray count];
    return [self.versionsArray count];
}


#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
         titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == departments)
          return [[self.modelsArray    objectAtIndex:row]objectForKey:@"model"];
    return [[[self.versionsArray objectAtIndex:row]objectForKey:@"model"]objectForKey:@"version"];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:    (NSInteger)component
{
    if (component == departments)
    {
        NSString * selectedRow = [[self.modelsArray objectAtIndex: row]    valueForKey:@"version"];
        NSArray * array = [sybreDepts objectForKey:selectedRow];
        self.
    }
}

@end

Basically when I select a model i.e juice or cream on the first column the second column should display its contents with key being ‘versions’ thus when juice is selected “version01” and “version02” should be displayed on the second column.

In regards to the line return [[[self.versionsArray objectAtIndex:row]objectForKey:@"model"]objectForKey:@"version"]; I don’t believe it was executed correctly as I get an error

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0xa18c450' . Any suggestions ? Thanks
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:49:11+00:00Added an answer on June 16, 2026 at 9:49 pm

    If you nicely indent your JSON data and look at it:

    [
        {
            "model": "juice",
            "id" : [
                {
                    "version": "version01"
                }, 
                {
                    "version": "version02"
                }
            ]
        },
        ....
    

    then you’ll see that it has the following structure (from the outer to the inner elements):

    1. an array
    2. a object/dictionary with two keys: model and version
    3. a string for the key model and an object/dictionary for the key id
    4. (for the key id only) a string for the key version

    So if you want to access the second version of the first model, you have to write something like this:

    NSArray* versionsArray = [[self.modelsArray objectAtIndex:modelRow] objectForKey:@"id"];
    NSString* versionName = [[versionArray objectAtIndex: versionRow] objectForKey:@"version"];
    

    In your code, I can’t see where self.versionArray is ever assigned a value. And as I’ve pointed out in my comment, the following line doesn’t match the JSON data structure:

    return [[[self.versionsArray objectAtIndex:row]objectForKey:@"model"]objectForKey:@"version"];
    

    The part:

    [[self.versionsArray objectAtIndex:row]objectForKey:@"model"]
    

    will return a NSString instance. And if you then call objectForKey:@"version", you’ll get the unrecognized selector sent error.

    Update:

    The changed code could look like this:

    .h file:

    NSInteger modelRow;
    

    .m file:

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 2;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        if (component == departments)
            return [self.modelsArray count];
    
        NSArray* versionsArray = [[self.modelsArray objectAtIndex:modelRow] objectForKey:@"id"];
        return [versionsArray  count];
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        if (component == departments)
              return [[self.modelsArray objectAtIndex:row]objectForKey:@"model"];
    
        NSArray* versionsArray = [[self.modelsArray objectAtIndex:modelRow] objectForKey:@"id"];
        return [[versionsArray objectAtIndex:row] objectForKey:@"version"];
    }
    
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:    (NSInteger)component
    {
        if (component == departments)
        {
            modelRow = row;
            [pickerView reloadComponent:1];
        }
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three labels, which I want to fill with selected row from pickerview.
I have a weird issue: I'm using a UIPickerView to pick some values. When
I have created a sample picker-view which will display some details. Setting the Background
I want to display PickerView when I click the button. I have a view
I have an application which I am developing using WPF\Prism\MVVM. All is going well
I have a pickerView which has a component of colors. I have a textField
I want to display UIPickerView with UIToolBar on the top of pickerView with done
i have added a pickerview from interface builder.i am using this method to show
I am using Xcode 4.3.1 . I have created the UI using Storyboard which
I am using Xcode 4.3.1 . I have created the UI using Storyboard which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.