Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ahan.tm

macrumors regular
Original poster
Jun 26, 2011
141
0
Florida
Hi,

I have an array that supposedly contains multiple dictionaries. I get this data from JSON and was wondering how to extract a single dictionary out of the array.

Here is how I am currently set up:

Code:
NSError *error;
NSDictionary *json = [NSJSONSerialization
	                      JSONObjectWithData:responseData
	                      options:NSJSONReadingMutableContainers
	                      error:&error]; //Parse JSON Data

NSDictionary *planesInRange = [json objectForKey:@"SearchBirdseyeInFlightResult"]; //Create Dictionary

NSMutableArray *aircraft = [[NSMutableArray alloc] init];
    
[aircraft addObject:[planesInRange objectForKey:@"aircraft"]];
   
NSMutableDictionary *currentPlane = [[NSMutableDictionary alloc] init];

	
for (int i = 0; i <= ([aircraft count] - 1); i++) { 

          currentPlane = [aircraft objectAtIndex:i];

//Some More Stuff

}

When I run this code, it crashes at
Code:
currentPlane = [aircraft objectAtIndex:i];

Is there another way to do this, or to see what is contained inside the array???

Thanks! :)
 
Debugging means confirming your expectations. You expect something to happen. What if it doesn't? What if you get a different result than what you expected? Does what you expect to happen actually happen?

Break it down for each line of code. Confirm each expectation, and make sure you have a useful result before proceeding to the next line of code.


What happens if planesInRange is nil? For example, if objectForKey:mad:"SearchBirdseyeInFlightResult" finds no such key.

What happens if [planesInRange objectForKey:mad:"aircraft"] returns nil? How many objects will be in 'aircraft' as a result?

When it crashes, what is the exception being thrown? If it's not an exception, then exactly what causes the crash?

If you don't know the answers, then use the debugger. Set a breakpoint, step through the lines, see what the variables are.

At every line, you should be able to say "I expect ____ to happen", and fill in a very specific expectation, maybe even more than one. I suggest doing this for each line, with a pencil and paper, before starting the debugger. Then use the debugger to confirm each expectation. If the first expectation doesn't match what you can see with the debugger, figure out how to change it, either by fixing the code to match your expectation, or altering your expectation to match the reality.

If you don't know how to use the debugger, this is a great place to learn. The code is a very simple sequence of very simple lines. You should be able to set a breakpoint and step through line by line. Those should be two simple tasks that you can learn to do with any decent debugger tutorial. If you learn only those two tasks, you should be well equipped to do a lot of normal everyday debugging.
 
Debugging means confirming your expectations. You expect something to happen. What if it doesn't? What if you get a different result than what you expected? Does what you expect to happen actually happen?

Break it down for each line of code. Confirm each expectation, and make sure you have a useful result before proceeding to the next line of code.


What happens if planesInRange is nil? For example, if objectForKey:mad:"SearchBirdseyeInFlightResult" finds no such key.

What happens if [planesInRange objectForKey:mad:"aircraft"] returns nil? How many objects will be in 'aircraft' as a result?

When it crashes, what is the exception being thrown? If it's not an exception, then exactly what causes the crash?

If you don't know the answers, then use the debugger. Set a breakpoint, step through the lines, see what the variables are.

At every line, you should be able to say "I expect ____ to happen", and fill in a very specific expectation, maybe even more than one. I suggest doing this for each line, with a pencil and paper, before starting the debugger. Then use the debugger to confirm each expectation. If the first expectation doesn't match what you can see with the debugger, figure out how to change it, either by fixing the code to match your expectation, or altering your expectation to match the reality.

If you don't know how to use the debugger, this is a great place to learn. The code is a very simple sequence of very simple lines. You should be able to set a breakpoint and step through line by line. Those should be two simple tasks that you can learn to do with any decent debugger tutorial. If you learn only those two tasks, you should be well equipped to do a lot of normal everyday debugging.

Thank You For Your Influential Response!! It now works when I changed this:

Code:
 [aircraft addObject:[planesInRange objectForKey:@"aircraft"]];

to:

Code:
    [aircraft addObjectsFromArray:[planesInRange objectForKey:@"aircraft"]];

It seems that addObject was actually crashing, not when I set my dictionary.

Thanks For All Your Help!! :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.