Я новичок в Objective-C, и мои навыки C/C++ довольно ржавые. Какое лучшее время для изучения разработки iOS (!)Объективные C-блоки, переменные и CLGeocoder и/или CLPlacemark
Я пытаюсь изменить геолокацию позиции с использованием класса CLGeocoder в iOS. Я могу успешно получить данные, которые меня интересуют (адрес улицы) внутри блока/обратного вызова, однако, когда я пытаюсь использовать эти данные для заполнения моей переменной (вне блока), данных там нет. Как будто объект в блоке исчезает до того, как объект MapView вызывает его. Я использую __block, который, как я понимаю, должен позволить переменной сохраняться за пределами блока, но, похоже, это не так.
Вот код в вопросе:
- (void) foundLocation:(CLLocation *)loc
{
CLLocationCoordinate2D coord = [loc coordinate];
// Get our city and state from a reversegeocode lookup and put them in the subtitle field (nowString).
// reversegeocode puts that information in a CLPlacemark object
// First, create the CLGeocoder object that will get us the info
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
// Next create a CLPlacemark object that we can store what reverseGeocodeLocation will give us containing the location data
__block CLPlacemark *placemark = [[CLPlacemark alloc]init];
__block NSString *sPlacemark = [[NSString alloc]init];
// This next bit is where things go awry
[geocoder reverseGeocodeLocation:loc completionHandler:
^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0)
{
placemark = [placemarks objectAtIndex:0];// this works!!
sPlacemark = [placemark thoroughfare]; // as does this! I can see the street address in the variable in the debugger.
}
}];
MapPoint *mp = [[MapPoint alloc] initWithCoordinate:coord
title:[locationTitleField text]
subtitle:sPlacemark];
// add it to the map view
[worldView addAnnotation:mp];
// MKMapView retains its annotations, we can release
[mp release];
// zoom region to this location
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
[worldView setRegion:region
animated:YES];
[locationTitleField setText:@""];
[activityIndicator stopAnimating];
[locationTitleField setHidden:NO];
[locationManager stopUpdatingLocation];
}
Я не полностью обернуты вокруг моей головы «блоков», так что, скорее всего, где проблема, но я не могу понять точно, что.
Помощь?
Заранее спасибо.
Супер полезно! Большое спасибо Фирозе. Мне особенно нравится ваш ресторанный аналог для обработчика завершения. Еще раз спасибо! – Nick
Рад, что помогло. Удачи с приложением. –