Ios 从JSON输出中分离纬度和经度值

Ios 从JSON输出中分离纬度和经度值,ios,iphone,objective-c,ios7,cllocation,Ios,Iphone,Objective C,Ios7,Cllocation,我有一组poly-line解码字符串,我将它们存储在一个数组中。不,我想从中分离纬度和经度值 我明白这一点。对于一根弦来说,它工作得很好。如果我想从多个服务器获取Lat-Long值,它会抛出一个异常 这是我写的代码: NSMutableArray *locArr=[NSMutableArray array]; for(NSDictionary *dict in polylineArr) { NSMutableString *str=[dict valueForKey:@"poi

我有一组poly-line解码字符串,我将它们存储在一个数组中。不,我想从中分离纬度和经度值

我明白这一点。对于一根弦来说,它工作得很好。如果我想从多个服务器获取Lat-Long值,它会抛出一个异常

这是我写的代码:

 NSMutableArray *locArr=[NSMutableArray array];

 for(NSDictionary *dict in polylineArr)
 {
     NSMutableString *str=[dict valueForKey:@"points"];
     NSArray *Arr=[self decodePolyLine:str];
     [locArr addObjectsFromArray:Arr];
 }
 NSLog(@"%@",locArr);
 for(int i=0;i<[locArr count];i++)
 {
     NSLog(@"%@",[locArr objectAtIndex:i]);

     NSMutableString * str = [[NSMutableString alloc]init];
     str = [locArr objectAtIndex:i];

     NSLog(@"%@",str);
     str = [[str componentsSeparatedByString:@">"] objectAtIndex:0];

     str = (NSMutableString *)[str substringFromIndex:1];

     NSString* strLat = (NSString*)[[str componentsSeparatedByString:@","] objectAtIndex:0];
     NSString* strLon = (NSString*)[[str componentsSeparatedByString:@","] objectAtIndex:1];
     NSLog(@"%@",strLat);
     NSLog(@"%@",strLon);
}
NSMutableArray*locArr=[NSMutableArray];
用于(NSDictionary*多段线中的dict)
{
NSMutableString*str=[dict valueForKey:@“points”];
NSArray*Arr=[自解码多段线:str];
[locArr addObjectsFromArray:Arr];
}
NSLog(@“%@”,locArr);

对于(int i=0;i您正在将ComponentSeparatedByString消息发送到CLLocation类型的变量,即使您认为它是一个NSString。我建议稍微取消嵌套代码。而不是执行以下操作:

NSString* strLat = (NSString*)[[str componentsSeparatedByString:@","] objectAtIndex:0];
将它分成多行,在这些行中,您实际将要提取的每个内容分配给适当类型的变量。这将有助于发现(并防止)类似的错误


希望这有帮助。

我觉得这很可疑

str = [locArr objectAtIndex:i];
NSLog(@"%@",str);
str = [[str componentsSeparatedByString:@">"] objectAtIndex:0];

您正试图调用CLLocation对象中的string方法。locArr包含NSString或CLLocation项?

您看到的错误消息中实际诊断出错误:

-[CLLocation componentsSeparatedByString:]: unrecognized selector sent to instance 0x1309edd0
这表示您已将
-组件通过字符串分隔:
发送到
CLLocation
对象,当然,
CLLocation
不会响应该消息。您没有说异常发生在哪一行,但我猜是这一行:

str = [locArr objectAtIndex:i];
locArray
包含
CLLocation
对象,而不是
NSString
对象


还有几个观察结果:

导致错误的线上方的线

NSMutableString * str = [[NSMutableString alloc]init];
是冗余的。它分配一个新的
NSMutableString
,并将
str
设置为指向它的指针。但是
str
会立即在下一行被覆盖

最后,您的for循环可以替换为快速迭代:

for (CLLocation* location in locArray)
{
    // All the stuff inside
}

以下是我在代码中的实现方式-适用于您的情况:

头文件:

@interface CLLocation (String)

+ (instancetype) clLocationWithString:(NSString *)location;

@end
以及实施:

@implementation CLLocation (String)

+ (instancetype)clLocationWithString:(NSString *)location
{
    static NSRegularExpression *staticRegex;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSError *error = NULL;
        //ex: <41.081445,-81.519005> ...
    staticRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\-?\\d+\\.?\\d*)+"
                                                            options:NSRegularExpressionCaseInsensitive
                                                              error:&error];
    });

    NSArray *matches = [staticRegex matchesInString:location options:NSMatchingReportCompletion range:NSMakeRange(0, location.length)];

    if (matches.count >= 2) {
        return [[CLLocation alloc] initWithLatitude:[[location substringWithRange:((NSTextCheckingResult *)[matches objectAtIndex:0]).range] doubleValue]
                                          longitude:[[location substringWithRange:((NSTextCheckingResult *)[matches objectAtIndex:1]).range] doubleValue]];
    } else {
        return [[CLLocation alloc] init];
    }
}
@实现位置(字符串)
+(instancetype)clLocationWithString:(NSString*)位置
{
静态NSRegularExpression*staticRegex;
静态调度一次;
一次发送(一次发送)^{
n错误*错误=NULL;
//例:。。。
staticRegex=[NSRegularExpression regular expression with pattern:@“(\\-?\\d+\.?\\d*)+”
选项:nsregularexpressioncase不敏感
错误:&错误];
});
NSArray*matches=[staticRegex MatchesInstalling:位置选项:NSMatchingReportCompletion范围:NSMakeRange(0,location.length)];
如果(matches.count>=2){
返回[[CLLocation alloc]initWithLatitude:[[location substringWithRange:((NSTextCheckingResult*)[matches objectAtIndex:0])。range]doubleValue]
经度:[[location substringWithRange:((NSTextCheckingResult*)[matches objectAtIndex:1]).range]doubleValue];
}否则{
返回[[CLLocation alloc]init];
}
}

要么
[dict valueForKey:@“points”]
结果是
CLLocation
的类型,而不是
NSString
,要么
[self-decodePolyLine:str]
返回包含
CLLocation
对象的数组。您能同时提供那些
NSLog
-s吗?
@implementation CLLocation (String)

+ (instancetype)clLocationWithString:(NSString *)location
{
    static NSRegularExpression *staticRegex;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSError *error = NULL;
        //ex: <41.081445,-81.519005> ...
    staticRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\-?\\d+\\.?\\d*)+"
                                                            options:NSRegularExpressionCaseInsensitive
                                                              error:&error];
    });

    NSArray *matches = [staticRegex matchesInString:location options:NSMatchingReportCompletion range:NSMakeRange(0, location.length)];

    if (matches.count >= 2) {
        return [[CLLocation alloc] initWithLatitude:[[location substringWithRange:((NSTextCheckingResult *)[matches objectAtIndex:0]).range] doubleValue]
                                          longitude:[[location substringWithRange:((NSTextCheckingResult *)[matches objectAtIndex:1]).range] doubleValue]];
    } else {
        return [[CLLocation alloc] init];
    }
}