Question

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?

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);
        }
    } ];
}
Was it helpful?

Solution

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 ...

UPDATE:

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];
    }
}

Do same like another 3 field and at last set that value to the UILable like bellow...

placeLabel.text = strLblTexts;

OTHER TIPS

Two options:

1) Use an NSMutableString and only append the non-nil values

2) Update your current solution so each parameter is like:

placemark.country ? placemark.country : @"", placemark.subAdministrativeArea ? placemark.subAdministrativeArea : @"", ...

Update: I didn't notice the commas in the original question. Due to those being there, your best option is option 1:

NSMutableString *label = [NSMutableString string];
if (placemark.country) {
    [label appendString:placemark.country];
}
if (placemark.subAdministrativeArea) {
    if (label.length) {
        [label appendString:@", "];
    }
    [label appendString:placemark.subAdministrativeArea];
}
// and the rest
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top