1

ユーザーの場所を見つけてそれをラベルに返すコードがいくつかあります。場合によっては、subAdministrativeArea が利用できないか、大通りが利用できないため、文字列を変更して表示されないようにしたい (null)。これを確認するためにいくつか試してみましたが、(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);
        }
    } ];
}
4

2 に答える 2

2

これを試してみてください.. if の場合placemark.subAdministrativeAreanil、if 条件に独自の文字列を書き込むことができます。それ以外の場合は、値を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;
于 2013-04-15T07:27:35.310 に答える
2

2 つのオプション:

1) を使用しNSMutableString、非 nil 値のみを追加します

2) 現在のソリューションを更新して、各パラメーターが次のようになるようにします。

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

更新:元の質問のコンマに気づきませんでした。それらが存在するため、最適なオプションはオプション 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
于 2013-04-15T07:25:49.700 に答える