【问题标题】:locationManager avoid (null) string in a LabellocationManager 避免标签中的(空)字符串
【发布时间】:2013-04-15 07:22:00
【问题描述】:

我有一些代码可以找到用户位置并将其返回到标签中。在某些情况下,subAdministrativeArea 或通道不可用,我想修改我的字符串,使其不显示 (null)。我尝试了一些 if 来检查这一点,但如果 (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);
        }
    } ];
}

【问题讨论】:

  • 你好,请详细提问。那应该有帮助..

标签: ios cllocationmanager stringwithformat


【解决方案1】:

试试这个..如果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;

【讨论】:

  • 如果我尝试使用这个并且它是 nil,我会有一个逗号和一个空格。如果地标为零,我最好删除逗号。
  • 一个更简洁的版本是:NSString *subAdministrativeArea = placemark.subAdministrativeArea ? placemark.subAdministrativeArea : @"";
  • 顺便说一句 - 这是无效的:if ([placemark.subAdministrativeArea isEqual:nil]) {。您实际上想要:if (placemark.subAdministrativeArea == nil) { 或:if (!placemark.subAdministrativeArea) {
  • @rmaddy 是的,它很简单,也只需一行就完成了......我没有实现它,因为你已经在你的答案中发布了那种类型的登录...... :)
  • @patapple 您要从中删除逗号??
【解决方案2】:

两种选择:

1) 使用NSMutableString 并且只附加非零值

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

【讨论】:

  • 我对第二个选项感兴趣。你能详细说明这些语法是什么吗?
  • ?: 运算符是标准 C 运算符。一般是<condition> ? <true value> : <false value>
  • @patapple 查看我的更新。我第一次回答时没有注意到逗号。由于nil 值,您不想要额外的逗号,因此我的第一个选择是更好的选择。它更冗长,但它会给你正确的结果。
  • 抱歉,我不明白如何使用 NSMutableString。你为什么要这样做 if (placemark.country) 和 if(label.length) ?
  • 我认为 appendString 是最好的方法。需要弄清楚如何为我的整个代码实现它:)
猜你喜欢
  • 2015-06-13
  • 1970-01-01
  • 2014-11-22
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
相关资源
最近更新 更多