Question

I am initialising the following NSArray using JSON;

categorias = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];

I can retrieve the number of objects in the array logging it:

NSLog(@"NUMERO DE EMPRESAS = %lu", (unsigned long)[categorias count]);

The expected number of objects is 24, the log is correct.

NUMERO DE EMPRESAS = 24

Now, what I need is to iterate the 24 objects and show the value for the key 'latitud'

for (int i=0; i<[categorias count]; i++) {
    NSString *lalitudText = x; 
    NSLog (@"EL VALOR ES %@", latitudText);  
}

What should I put instead of x, to obtain the value?

Was it helpful?

Solution

Using the old syntax you can put this:

NSString *lalitudText = [[categorias objectAtIndex:i] objectForKey:@"latitude"]; 

The new syntax lets you do this:

NSString *lalitudText = categorias[i][@"latitude"]; 

OTHER TIPS

Use:

NSString *lalitudText = [categorias objectAtIndex:i]; 

Use fast enumeration:

for (NSDictionary *element in categorias) {
    NSLog (@"EL VALOR ES %@", element[@"lalitud"]);
}

Of course you have to adjust the key of the "lalitud" thingy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top