OGeek|极客世界-中国程序员成长平台

标题: ios - 从数组中查找地址并将其发送到其他 View [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:23
标题: ios - 从数组中查找地址并将其发送到其他 View

我正在尝试做简单的事情。我想找到一组地址并将其显示在 map 上。但是我需要将数据传递给另一个 View 。

问题是:我需要从包含地址的字典中传递数据,所以,我需要知道找到了哪个地址。请求是异步的,而且只有主线程,所以我现在不明白找到了什么地址。

对不起,如果我说的不清楚。

   for (NSDictionary *dic in adresses) {

            MKLocalSearch *search = [[MKLocalSearch alloc]initWithRequest:request];
            request.naturalLanguageQuery = [dic valueForKey"adress"];
            [search startWithCompletionHandler:^(MKLocalSearchResponse
                                                 *response, NSError *error) {


                if (response.mapItems.count == 0){

                    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
                    [dictionary setValue:request.naturalLanguageQuery forKey"realadress"];
                    [dictionary setValue"empty" forKey"itemname"];
                    [adressesonmap addObject:dictionary];
                    [array addObject"empty"];

//HERE I NEED TO KNOW WHICH ADRESS MAPKIT TRIED TO FIND!!!
                }
                else{
                    for (MKMapItem *item in response.mapItems)
                    {
                        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
                        [dictionary setValue:request.naturalLanguageQuery forKey"realadress"];
                        [dictionary setValue:item.name forKey"itemname"];
                        [adressesonmap addObject:dictionary];

                        [matchingItems addObject:item];
                        MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
                        annotation.coordinate = item.placemark.coordinate;
                        annotation.title = request.naturalLanguageQuery;
//HERE I NEED TO KNOW WHICH ADRESS MAPKIT TRIED TO FIND!!!

     // here I'me trying to get address, which I tried to find before in request, but it's always one of them (from request.naturalLanguageQuery)

                        [_mapView addAnnotation:annotation];
                        }
                    }
                }];

            }



Best Answer-推荐答案


据我了解,您需要知道异步请求找到了什么地址。 您正在使用 block 来获取回调,因此您可以只使用 block 中外部范围的变量,它将捕获引用直到完成。

NSArray *adresses = @[
        @{@"adress""Kyiv, Gorkogo 17"},
        @{@"adress""New York"}
];

for (NSDictionary *dic in adresses) {
    MKLocalSearchRequest *request = [MKLocalSearchRequest new];
    request.naturalLanguageQuery = [dic valueForKey"adress"];
    MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
    [search startWithCompletionHandler:^(MKLocalSearchResponse
                                         *response, NSError *error) {
        NSLog(@"\n\n\n+++++\nFound address: %@  response items:%@ ",dic[@"adress"], response.mapItems);
    }];

}

关于ios - 从数组中查找地址并将其发送到其他 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30457911/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4