我有一些代码可以找到用户位置并将其返回到标签中。在某些情况下,subAdministrativeArea 或通道不可用,我想修改我的字符串,使其不显示 (null)。我尝试了一些 if 来检查这一点,但如果 (null) 不止一个,则会出现问题。有人对此有任何想法吗?
这是我当前的代码,需要修改以检测地标对象是否等于 null:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocation *currentLocation = newLocation;
[location stopUpdatingLocation];
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
countryDetected = placemark.ISOcountryCode;
placeLabel.text = [NSString stringWithFormat:@"%@, %@, %@, %@, %@ %@", placemark.country, placemark.subAdministrativeArea, placemark.subLocality, placemark.postalCode, placemark.thoroughfare, placemark.subThoroughfare];
userPlace = [NSString stringWithFormat:@"%@, %@, %@, %@, %@ %@", placemark.country, placemark.subAdministrativeArea, placemark.subLocality, placemark.postalCode, placemark.thoroughfare, placemark.subThoroughfare];
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
}