I have an array (called results):
@interface FourViewController : UIViewController
{
NSArray *results;
NSMutableData *data;
}
NSURL *url = [NSURL URLWithString:@"http://website/json.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData alloc] init];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
results = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
}
When I run this for loop it returns all keys in the array correctly with each JSON string in:
int i;
for (i = 0; i < [results count]; i++) {
NSLog(@"Result: %i = %@", i, results[i]);
}
However inside the for loop I need something like this but I cannot find documentation to do it:
int i;
for (i = 0; i < [results count]; i++) {
location.latitude = results[i][lat];
location.longitude = results[i][long];
myAnn.coordinate = location;
myAnn.title = results[i][title];
myAnn.subtitle = results[i][strap];
[locations addObject:myAnn];
}
lat, long, title and strap are all JSON keys / id's but i cannot access them; is there a way for me to access each key within each iteration inside the array?
results? Is it an instance ofNSArrayorNSDictionary?resultsas anNSArray, you might not be getting that at runtime fromJSONObjectWithData(welcome to Objective C) depending on how the JSON data is structured. You will want to validate that.