简体   繁体   English

从iOS中的数组获取字典值?

[英]Get dictionary value from array in iOS?

I have the following NSArray : 我有以下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? 从这个我怎么能得到的latitudelongitude从各指标?

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
            }
        }
    }
}
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 您必须创建并分配latAry和longAry

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM