我有一些代码可以找到用户位置并将其返回到Label中。在某些情况下,subAdministrativeArea不可用或通途,我想修改我的字符串,使其不显示(空)。我尝试了一些是否检查这一点,但如果(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);
}
} ];
}
最佳答案
试试这个..如果placemark.subAdministrativeArea
是nil
,那么您可以编写自己的字符串,如果条件另有设置,则将placemark.subAdministrativeArea
的值设置为字符串变量,然后将其分配给UILable
...
更新:
NSMutableString *strLblTexts = [[NSMutableString alloc] init];
if (placemark.country != nil) {
[strLblTexts appendString:placemark.country];
}
if (placemark.subAdministrativeArea != nil) {
if ([strLblTexts isEqualToString:@""]||[strLblTexts isEqual:nil]) {
[strLblTexts appendString:placemark.subAdministrativeArea];
}
else{
NSString *strtemp=[NSString stringWithFormat:@",%@",placemark.subAdministrativeArea];
NSLog(@">>>>>>>>>>>>> str temp :%@", strtemp);
[strLblTexts appendString:strtemp];
}
}
if (placemark.subLocality != nil) {
if ([strLblTexts isEqualToString:@""]||[strLblTexts isEqual:nil]) {
[strLblTexts appendString:placemark.subLocality];
}
else{
NSString *strtemp=[NSString stringWithFormat:@",%@",placemark.subLocality];
NSLog(@">>>>>>>>>>>>> str temp :%@", strtemp);
[strLblTexts appendString:strtemp];
}
}
像其他3个字段一样进行操作,最后像波纹管一样将该值设置为
UILable
...placeLabel.text = strLblTexts;
关于ios - locationManager避免Label中的(空)字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16009729/