Get dictionary value from array in iOS?

I have the following NSArray:

(
        {
        geometry =         {
            location =             {
                lat = "37.789632";
                lng = "-122.417004";
            };
        };
        icon = "http://maps.gstatic.com/mapfiles/place_api/icons/doctor-71.png";
  vicinity = "900 Hyde Street, San Francisco";
    },
        {
        geometry =         {
            location =             {
                lat = "37.794774";
                lng = "-122.398941";
            };
        };
        icon = "http://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png";
 vicinity = "2 Embarcadero Center, San Francisco";
}
)

From this how can I get the latitude and longitude from each index?

Answers


for (int i=0 ; i<[parsedAry count] ; i++) {

    NSMutableDictionary * location = [[[parsedAry objectAtIndex:i]objectForKey:@"geometry"]objectForKey:@"location"];
    NSString * lat = [location objectForKey:"lat"];
    NSString * lng = [location objectForKey:"lng"]; 
    [latAry addObject:lat];
    [LongAry addObject:lng];


}

You have to create and allocate latAry and longAry


You didn't even try to think this one out?? I'll help you out for the funs, but help yourself man

NSArray *values = ...;
for (NSDictionary *geometry in values) {
    NSDictionary *loc = [geometry valueForKey:@"location"];
    if (loc) {
        NSNumber *lat = [loc valueForKey:@"lat"];
        NSNumber *lng = [loc valueForKey:@"lng"];
        if (lat && lng) {
            // do whatever
        }
    }
}

EDIT: maybe it's like this

NSArray *values = ...;
for (NSDictionary *item in values) {
    NSDictionary *geo = [item valueForKey:@"geometry"];
    if (geo) {
        NSDictionary *loc = [geometry valueForKey:@"location"];
        if (loc) {
            NSNumber *lat = [loc valueForKey:@"lat"];
            NSNumber *lng = [loc valueForKey:@"lng"];
            if (lat && lng) {
                // do whatever
            }
        }
    }
}

Need Your Help

Opinion: in HTML, Possible Duplicate IDs or Non-Standard Attributes?

javascript html database

It seems pretty common to want to let your javascript know a particular dom node corresponds to a record in the database. So, how do you do it?

Mysql Inner Join Issues

mysql subquery inner-join

This gives me all my articles that contain both the mentioned entities.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.