locationManager避免Label中的(空)字符串 [英] locationManager avoid (null) string in a Label
问题描述
我有一些代码可以找到用户位置并将其返回到Label中。在某些情况下,subAdministrativeArea不可用或通途,我想修改我的字符串,使其不显示(空)。我尝试了一些是否检查这一点,但如果(null)大于一个,则存在问题。有人对此有想法吗?
I have some code that find the user location and return it into a Label. In some case, the subAdministrativeArea is not available or the thoroughfare and I want to modify my string so it doesn't show (null). I tried with some if to check this but there is a problem if the (null) are more than one. Anyone has an idea about this?
这是我当前的代码,需要对其进行修改以检测地标对象是否等于null:
Here is my current code that needs to be modified to detect if a placemark object is equal to 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
...
try this one.. if placemark.subAdministrativeArea
is nil
then you can write your own string in if condition otherwise set the value of placemark.subAdministrativeArea
to the string variable and assign that to 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
的值...
Do same like another 3 field and at last set that value to the UILable
like bellow...
placeLabel.text = strLblTexts;
这篇关于locationManager避免Label中的(空)字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!