【问题标题】:[__NSCFNumber rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0xb000000000005f53[__NSCFNumber rangeOfCharacterFromSet:]:无法识别的选择器发送到实例 0xb000000000005f53
【发布时间】:2018-01-17 12:34:57
【问题描述】:

我正在尝试使用方法将我的 lat long 转换为地址:

#pragma mark:convert latLong to address
-(void)getMerchantAddress:(NSDictionary *)dict
{

    double lata = [[dict valueForKey:@"user_lat"] doubleValue];
    double longa = [[dict valueForKey:@"user_long"] doubleValue];

    NSLog(@"%f",lata);
    NSLog(@"%f",longa);

    CLLocation *LocationAtual = [[CLLocation alloc]initWithLatitude:lata longitude:longa];


    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder reverseGeocodeLocation:LocationAtual completionHandler:^(NSArray *placemarks, NSError *error)
     {
         if(placemarks && placemarks.count > 0)
         {
             CLPlacemark *placemark= [placemarks objectAtIndex:0];
             NSArray *lines = placemark.addressDictionary[ @"FormattedAddressLines"];
             NSString *addressString = [lines componentsJoinedByString:@","];
             _usrtAddressLbl.text = addressString;
             _userAddressLbl.text = addressString;
         }
     }];
}

我的应用程序崩溃@点

[geocoder reverseGeocodeLocation:LocationAtual completionHandler:^(NSArray *placemarks, NSError *error)

显示错误:

[__NSCFNumber rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0xb000000000005f53

我该如何解决。提前致谢。

【问题讨论】:

  • 使用 NSLog 打印什么?
  • @PrashantTukadiya 以正确的格式显示 lat long
  • 做到了。你检查完成块是否执行?
  • 错误信息很清楚:在某个地方需要一个字符串 (NSString),但传递了一个数字 (NSNumber)。使用调试器。设置断点并观察变量。并且不要使用valueForKey,除非你能解释为什么明确需要 KVC。
  • @V-Dev 能否请您显示您的 lata 和 long 值?并使用 objectForKey 而不是 valueForKey。

标签: ios objective-c iphone geocoding cllocation


【解决方案1】:

试试这个对我有用

-(void)getMerchantAddress:(NSDictionary *)dict
{


    float lata = [[dict objectForKey:@"user_lat"] floatValue];
    float longa = [[dict objectForKey:@"user_long"] floatValue];

    CLGeocoder *ceo = [[CLGeocoder alloc]init];
    CLLocation *loc = [[CLLocation alloc]initWithLatitude:lata longitude:longa]; //insert your coordinates

    [ceo reverseGeocodeLocation:loc
              completionHandler:^(NSArray *placemarks, NSError *error) {
                  CLPlacemark *placemark = [placemarks objectAtIndex:0];
                  if (placemark) {

                      NSLog(@"placemark %@",placemark);
                      //String to hold address
                      strAddress = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                      NSLog(@"addressDictionary %@", placemark.addressDictionary);

                  }else {

                  }
              }
     ];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 2015-03-03
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多