1

I have some data in JSON format which looks like this:

{"YrMonth":201109,"StrYrMonth":"Sep-2011","Value":"49275.14"}
{"YrMonth":201110,"StrYrMonth":"Oct-2011","Value":"52087.22"}
etc...

Using NSJSONSerialization I stored this data in an array called "indicator". When I want to use this data and display only the items with the key "Value" in a tableView, I can do it fine with:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

    cell.textLabel.text = [[indicator objectAtIndex:indexPath.row] objectForKey:@"Value"];
    return cell;
}

My question is: how can I create an Array that I can use outside of cellForRowAtIndexPath with the values for each key? For example:

An array for key "StrYrMonth" which contains only: "Sep-2011", "Oct-2011", etc...
An array for key "Value" which contains only: "49275.14", "52087.22", etc...

What I would ultimately like to do is use the "Value" array to plot a line in CorePlot and the "StrYrMonth" array to define de x-Axis labels of the graph.

Thanks in advance for your help!

1 Answer 1

3

You can use Key-Value Coding and the [NSArray valueForKey:] method (reference)

NSArray *strYrMonthArray = [indicator valueForKey:@"StrYrMonth"];
NSArray *valueArray = [indicator valueForKey:@"Value"];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! I knew the answer was going to be simple!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.