0
votes

I am not sure what's wrong with my code. Here are the 3 functions I have that might be problematic. It's a Google Maps app for iOS.

Error message: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI floatValue]: unrecognized selector sent to instance 0x60000042e680'

     - (void)drawPolygon{
    GMSMutablePath *rect = [GMSMutablePath path];
    for (int i = 0; i <= [tappedCoordinates count]-1; i++) {
    event.latitude = [[tappedCoordinates objectAtIndex:i] floatValue];
    event.longitude = [[tappedCoordinates objectAtIndex:i] floatValue];
    [rect addCoordinate:event];
}
// first tapped point to connect with last point in order to close the polygon.
event.latitude = [[tappedCoordinates objectAtIndex:0] floatValue];
event.longitude = [[tappedCoordinates objectAtIndex:0] floatValue];
[rect addCoordinate:event];
...
}

  - (void)addMarker{
for (int i = 0; i <= [tappedCoordinates count]-1; i++) {
position.latitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:0] floatValue];
position.longitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:1] floatValue];
...
}
1
One of the things you're calling floatValue on is an NSArray and not a string or number or whatever you're assuming it is. - dan
Actually, all of tappedCoordinates is an array. How do I fix this? - konyv12

1 Answers

1
votes

You're just not going deep enough into your array to get the actual long/lat values:

for (int i = 0; i <= [tappedCoordinates count]-1; i++) {
    event.latitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:0] floatValue];
    event.longitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:1] floatValue];
    [rect addCoordinate:event];
}

event.latitude = [[[tappedCoordinates objectAtIndex:0] objectAtIndex:0] floatValue];
event.longitude = [[[tappedCoordinates objectAtIndex:0] objectAtIndex:1] floatValue];

Edit: Just to clarify - [tappedCoordinates objectAtIndex:0] is an NSArray. And NSArray has no idea what to do when you call floatValue on it, since it's not a numerical value. Also, an alternative to your approach would be to have an array of CGPoint objects (slightly nicer implementation than nested arrays):

NSMutableArray *coords = [NSMutableArray new];
CGPoint coord = CGPointMake(1.5f, 2.5f);
[coords addObject:[NSValue valueWithCGPoint: coord]];

CGPoint storedCoord = [[coords objectAtIndex:0] CGPointValue];
NSLog (@"Lat: %f, long: %f", storedCoord.x, storedCoord.y);

You could even go a step further and create a subclass CGPoint (named Coordinate) which has the properties latitude and longitude which can return x and y respectively (largely for readability).