ios - 从数组中查找地址并将其发送到其他 View
<p><p>我正在尝试做简单的事情。我想找到一组地址并将其显示在 map 上。但是我需要将数据传递给另一个 View 。 </p>
<p>问题是:我需要从包含地址的字典中传递数据,所以,我需要知道找到了哪个地址。请求是异步的,而且只有主线程,所以我现在不明白找到了什么地址。</p>
<p>对不起,如果我说的不清楚。</p>
<pre><code> for (NSDictionary *dic in adresses) {
MKLocalSearch *search = [initWithRequest:request];
request.naturalLanguageQuery = ;
[search startWithCompletionHandler:^(MKLocalSearchResponse
*response, NSError *error) {
if (response.mapItems.count == 0){
NSMutableDictionary *dictionary = [init];
;
;
;
;
//HERE I NEED TO KNOW WHICH ADRESS MAPKIT TRIED TO FIND!!!
}
else{
for (MKMapItem *item in response.mapItems)
{
NSMutableDictionary *dictionary = [init];
;
;
;
;
MKPointAnnotation *annotation = [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)
;
}
}
}];
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>据我了解,您需要知道异步请求找到了什么地址。
您正在使用 block 来获取回调,因此您可以只使用 block 中外部范围的变量,它将捕获引用直到完成。</p>
<pre><code>NSArray *adresses = @[
@{@"adress":@"Kyiv, Gorkogo 17"},
@{@"adress":@"New York"}
];
for (NSDictionary *dic in adresses) {
MKLocalSearchRequest *request = ;
request.naturalLanguageQuery = ;
MKLocalSearch *search = [ initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse
*response, NSError *error) {
NSLog(@"\n\n\n+++++\nFound address: %@response items:%@ ",dic[@"adress"], response.mapItems);
}];
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 从数组中查找地址并将其发送到其他 View ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/30457911/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/30457911/
</a>
</p>
页:
[1]