2013-04-15 1 views
1

사용자 위치를 찾아 레이블로 반환하는 코드가 있습니다. 경우에 따라 subAdministrativeArea를 사용할 수 없거나 도로 정보가 표시되지 않으므로 문자열을 수정하려고합니다 (null). 나는 이것을 체크하기 위해 노력했지만, (null)이 둘 이상일 경우 문제가있다. 누구나 이것에 대해 생각해 봤어?locationManager 레이블에 피하기 (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); 
     } 
    } ]; 
} 
+0

안녕하세요, 정중하게 질문하십시오. 도움이 될 것입니다 .. –

답변

2

이 하나를 시도 변수 ... 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]; 
    } 
} 

이 같은 다른 3 필드처럼하고에 가치를 마지막 세트에서 수행좋아해 ...

placeLabel.text = strLblTexts; 
+0

이 코드를 사용하려고하면 nil이고 빈 칸 사이에 쉼표가 생깁니다. 장소 표시가 0이면 쉼표를 삭제하는 것이 좋습니다. – BalestraPatrick

+0

이 훨씬 더 간결한 버전은 다음과 같습니다 :'NSString * subAdministrativeArea = placemark.subAdministrativeArea? placemark.subAdministrativeArea : @ "";'. – rmaddy

+0

BTW - 유효하지 않습니다 :'if ([placemark.subAdministrativeArea isEqual : nil]) {'. 실제로 원하는 경우 : if (placemark.subAdministrativeArea == nil) {'또는 : if (! placemark.subAdministrativeArea) {'. – rmaddy

2

두 가지 옵션 :

1) NSMutableString를 사용하여 만 비를 추가 여기

는 장소 객체가 null에 동일한 경우는 감지하기 위해 수정 될 필요가 내 현재 코드입니다

placemark.country ? placemark.country : @"", placemark.subAdministrativeArea ? placemark.subAdministrativeArea : @"", ... 
: 전무는

2) 각각의 매개 변수처럼 현재의 솔루션을 업데이트 값3210

업데이트 : 원래 질문에서 쉼표를 발견하지 못했습니다. 거기 사람들로 인해, 당신의 최선의 선택 옵션 1 : placemark.subAdministrativeAreanil는 다음 조건이 다른 문자열에 placemark.subAdministrativeArea의 값을 설정 한 경우에 자신의 문자열을 쓸 수 있다면 ..

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 
+0

두 번째 옵션에 관심이 있습니다. 이 구문이 무엇인지 자세히 설명해 주시겠습니까? – BalestraPatrick

+0

'? :'연산자는 표준 C 연산자입니다. 일반적으로 '? : '. – rmaddy

+0

@patapple 내 업데이트를 참조하십시오. 내가 처음 대답했을 때 나는 쉼표에 주목하지 않았다. 'nil '값으로 인해 쉼표를 추가하지 않으려 고하기 때문에 첫 번째 옵션이 훨씬 더 나은 선택입니다. 훨씬 더 자세한 내용이지만 정확한 결과를 제공합니다. – rmaddy