如何从 Objective C 中的 NSArray 获取元素

How to fetch elements from NSArray in Objective C

我正在使用公交车时刻表API,并希望根据用户当前位置显示附近的公交车站。我已经通过 url 获得了 JSON 响应并将 JSON 解析为 NSArray。 NSArray 看起来像这样。

{
    result =         {
        distance = "0.00003292029";
        lat = "-37.92091";
        "location_name" = "Monash Medical Centre/Clayton Rd ";
        lon = "145.120682";
        "route_type" = 2;
        "stop_id" = 16518;
        suburb = Clayton;
        "transport_type" = bus;
    };
    type = stop;
},
    {
    result =         {
        distance = "0.00003728643";
        lat = "-37.92227";
        "location_name" = "Monash Surgical Private Hospital/291 Clayton Rd ";
        lon = "145.1202";
        "route_type" = 2;
        "stop_id" = 24348;
        suburb = Clayton;
        "transport_type" = bus;
    };
    type = stop;
},
    {
    result =         {
        distance = "0.00003804303";
        lat = "-37.9230766";
        "location_name" = "Clayton Railway Station/Clayton Rd ";
        lon = "145.120209";
        "route_type" = 2;
        "stop_id" = 22809;
        suburb = Clayton;
        "transport_type" = bus;
    };
    type = stop;
},
    {
    result =         {
        distance = "0.00003976311";
        lat = "-37.9186172";
        "location_name" = "Monash Specialist Centre/Clayton Rd ";
        lon = "145.121033";
        "route_type" = 2;
        "stop_id" = 22807;
        suburb = Clayton;
        "transport_type" = bus;
    };
    type = stop;
},
    {
    result =         {
        distance = "0.00004085229";
        lat = "-37.9186478";
        "location_name" = "Dixon St/Clayton Rd ";
        lon = "145.120911";
        "route_type" = 2;
        "stop_id" = 13889;
        suburb = Clayton;
        "transport_type" = bus;
    };
    type = stop;
}

)

我的问题是如何从这个 NSArray 中获取纬度和经度。我写了这些代码,但它 returns nil.

    NSMutableArray *coordinateLatit = [[NSMutableArray alloc]init];
for (int i = 0; i<nearbyStop.count; i++) {
    NSString *latit = [[nearbyStop objectAtIndex:1] objectForKey:@"lat"];
    if(latit == nil)
    {
        NSLog(@"there is no data");
    }
    else
    {
    [coordinateLatit addObject:latit];
    }
}

我还想在 MKMapView 上显示停靠点(使用 NSArray 的经度和纬度),我应该如何实现 MKLocalSearchRequest 如果有人能帮助我,我将不胜感激!

您错过了 json 回复的一个级别,应该是

for (Stop *stop in nearbyStop) {
    NSString *latit = [[stop objectForKey:@"result"] objectForKey:@"lat"];
    if(latit == nil)
    {
        NSLog(@"there is no data");
    }
    else
    {
        [coordinateLatit addObject:latit];
    }
}
-(id) doApiCall:(NSString*)apiCall useCache:(BOOL)useCache
{


   NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", mBaseURL, [MapViewController signApiCall:apiCall]]];

id response = [self httpWrapper:url];
    NSLog( @"%@", response );
return response;
}



-(id) httpWrapper:(NSURL*)url

{
    __block id response = nil;

void (^doStuff)() = ^void()
{
    int retry = 3;
    NSError * error;


    while( retry > 0) {
        NSData * data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url
                                                                                 cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                                             timeoutInterval:600] returningResponse:nil error:&error ];

        if( data != nil )
        {
            response = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            if( response != nil)
                retry = 0;
        }

        if(data == nil || response == nil)
        {
            NSLog(@"Bad response %@", url);
            NSLog(@"%@", error);
            --retry;
        }
    }
};


    doStuff();


    return response;
}

-(NSArray*) nearby:(CLLocation*)location
{

        return [self doApiCall:[NSString stringWithFormat:@"/v2/nearme/latitude/%f/longitude/%f?limit=5", location.coordinate.latitude, location.coordinate.longitude]];

}

这是我用来调用 API 的代码。我将 id 用于 url 请求的响应。附近的方法 returns NSArray 类型也是如此吗?

您的代码忽略了额外的嵌套 result 级别。

NSMutableArray *coordinateLatit=[[NSMutableArray alloc]init];

for(NSDictionary *d in nearbyStop)
{
    NSString    *latit=d[@"result"][@"lat"];

    if(lat) [coordinateLatit addObject:latit];
}