我遇到了最奇怪的问题,它正在引起我的注意。我在 Singleton 中设置的全局变量在它设置的函数中正确报告,然后在下一个函数中报告为 NULL(即我需要访问它的地方),但从另一个视图来看是正确的!所以变量设置正确,但它没有在某个函数中运行。违规行(我在*之间标记)还会生成一个奇怪的错误警告。
警告是:
Property access result unused - getters should not be used for side effects.
为非常参差不齐的代码道歉。我在进行原型设计和学习,所以这是我从网上拼凑出来的东西的混搭。代码所做的是识别地图视图上的长按,然后在该位置放置一个图钉(同时记录该位置),我正在尝试使用地理编码在图钉位置显示地址。
第一个函数如下:
- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:self.fullMapView];
CLLocationCoordinate2D touchMapCoordinate =
[self.fullMapView convertPoint:touchPoint toCoordinateFromView:self.fullMapView];
//save new birthplace in global variable
globalsSingle.gblBirthplace = touchMapCoordinate;
//place user location and record it
MKUserLocation *location = fullMapView.userLocation;
globalsSingle.gblCurrentLocation = location.coordinate;
//first remove any previous birthplace pin
[self removeAllPinsButUserLocation];
[self reverseGeocode];
//place new birthplace pin
MKPointAnnotation *birthPlacePin = [[MKPointAnnotation alloc] init];
birthPlacePin.coordinate = touchMapCoordinate;
birthPlacePin.title = @"My Birthplace";
**** birthPlacePin.subtitle = @"%@", globalsSingle.gblAddress; ****
[self.fullMapView addAnnotation:birthPlacePin];
NSLog(@"gblAddress = %@", globalsSingle.gblAddress);
}
上面的函数调用下一个:
-(void)reverseGeocode {
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:globalsSingle.gblBirthplace.latitude longitude:globalsSingle.gblBirthplace.longitude]; //insert your coordinates
[ceo reverseGeocodeLocation: loc completionHandler:
^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to hold address
NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
// save the address text
globalsSingle.gblAddress = locatedAt;
NSLog(@"addressDictionary %@", placemark.addressDictionary);
NSLog(@"placemark %@",placemark.region);
NSLog(@"placemark %@",placemark.country); // Give Country Name
NSLog(@"placemark %@",placemark.locality); // Extract the city name
NSLog(@"location %@",placemark.name);
NSLog(@"location %@",placemark.ocean);
NSLog(@"location %@",placemark.postalCode);
NSLog(@"location %@",placemark.subLocality);
NSLog(@"location %@",placemark.location);
//Print the location to console
NSLog(@"I am currently at %@",locatedAt);
NSLog(@"gblAddress from reverse Geocode = %@", globalsSingle.gblAddress);
}
];
}
更奇怪的是(对我来说)来自 reverseGeocode 的 NSLog 都正确打印,但是第一个函数的 NSLog 报告为 NULL,并且在 reverseGeocode 的那个之前打印,即使它(我假设)被执行第二个!例如,调试输出是:
2013-05-21 23:41:04.662 Project Name[5659:c07] gblAddress = (null)
2013-05-21 23:41:04.808 Project Name[5659:c07] gblAddress from reverse Geocode = Januária - MG, Brazil
任何人都可以提供任何帮助,我会很感激,因为我被迷惑了:)