Question

I am Having some set of Ploy-line Decoded Strings i store them in an array. No i want to separate Latitude and Longitude values from them.

I follow this this link. It is Working fine for one String. if i want to get Lat Long Values from more than one it is throwing an exception.

this is the code i write:

 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);
}

The Exception is:

Application[5228:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CLLocation componentsSeparatedByString:]: unrecognized selector sent to instance 0x1309edd0'

Console Output is:

 "<+16.30270004,+80.43871307> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30273056,+80.43869019> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30310059,+80.43843842> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30349922,+80.43820190> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30388069,+80.43795776> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30422020,+80.43772888> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30462074,+80.43750763> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30500031,+80.43731689> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30522919,+80.43720245> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30540085,+80.43711090> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30554008,+80.43702698> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30578041,+80.43691254> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30610085,+80.43673706> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30616951,+80.43669891> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30632019,+80.43662262> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30648041,+80.43653107> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30648041,+80.43653107> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30648994,+80.43652344> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30648994,+80.43650818> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30648994,+80.43650055> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30649948,+80.43649292> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30649948,+80.43647766> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30651093,+80.43647766> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30651093,+80.43647003> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30652046,+80.43647003> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30652046,+80.43646240> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30653000,+80.43646240> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30653000,+80.43644714> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30653954,+80.43644714> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30654907,+80.43643951> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30656052,+80.43643951> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30657005,+80.43643951> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30657005,+80.43643188> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30657959,+80.43643188> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30658913,+80.43643188> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30660057,+80.43643188> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30661011,+80.43643188> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30661964,+80.43643188> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30662918,+80.43643951> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30664062,+80.43643951> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30665016,+80.43644714> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30665970,+80.43644714> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30666924,+80.43646240> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30668068,+80.43647003> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30669022,+80.43647766> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30669022,+80.43649292> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30669975,+80.43649292> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30669975,+80.43650055> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30669975,+80.43650818> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time",
"<+16.30670929,+80.43652344> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time"
 )
 2014-03-21 16:56:01.952 Application[5228:70b] <+16.50616074,+80.64797211> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time
 2014-03-21 16:56:04.937 Application[5228:70b] <+16.50616074,+80.64797211> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/21/14, 4:55:56 PM India Standard Time
 2014-03-21 16:56:06.104 Application[5228:70b] -[CLLocation componentsSeparatedByString:]: unrecognized selector sent to instance 0x1309edd0
 2014-03-21 16:56:06.108 Application[5228:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CLLocation componentsSeparatedByString:]: unrecognized selector sent to instance 0x1309edd0'
 *** First throw call stack:

 libc++abi.dylib: terminating with uncaught exception of type NSException
Was it helpful?

Solution

The error is actually diagnosed in the error message you see:

-[CLLocation componentsSeparatedByString:]: unrecognized selector sent to instance 0x1309edd0

That is telling you that you have sent -componentsSeparatedByString: to aCLLocation object and of course, CLLocation doesn't respond to that message. You don't say which line the exception occurs on, but I would guess it is this one:

str = [locArr objectAtIndex:i];

and locArray contains CLLocation objects, not NSString objects.


A couple more observations:

The line above the line causing the error

NSMutableString * str = [[NSMutableString alloc]init];

is redundant. It allocates a new NSMutableString and sets str to be a pointer to it. But then str is overwritten immediately on the next line.

Finally, your for loop can be replaced by fast iteration:

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

OTHER TIPS

You are sending the componentsSeparatedByString message to a variable of type CLLocation even though you think it is an NSString. I would suggest un-nesting your code a bit. Instead of doing things like this:

NSString* strLat = (NSString*)[[str componentsSeparatedByString:@","] objectAtIndex:0];

separate it out into multiple lines where you actually assign each thing you are extracting to a variable of the proper type. This will help uncover (and prevent) bugs like this.

Hope this helps.

This looks suspicious to me.

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

you are trying to call a string method in CLLocation object. locArr contains NSString or CLLocation items?

This is how I do it in my code - adapted to your case:

Header file:

@interface CLLocation (String)

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

@end

And the implementation:

@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];
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top