Reputation: 4920
I have following method which is failing . I think I am comparing double with int but do know the best approach.
-(BOOL)isValidCoordinate:(CLLocationCoordinate2D)coordinate
{
//Always fail first condition here.
if (coordinate.latitude > -90 && coordinate.latitude< 90) {
[NSException raise:@"Invalid latitude value" format:@"Latitude of %d is invalid", coordinate.latitude];
}
if (coordinate.longitude > -180 && coordinate.longitude < 180) {
[NSException raise:@"Invalid longitude value" format:@"Longitude of %d is invalid", coordinate.longitude];
}
[NSException raise:@"Invalid longitude value blash " format:@"Longitude of %d is invalid asd", coordinate.longitude];
return TRUE;
}
Upvotes: 0
Views: 1136
Reputation: 1571
In this case, you don't need to check lat/long explicitly since MapKit has a method that do this to you. Try the following:
if (!CLLocationCoordinate2DIsValid(coordinate)) {
[NSException raise:@"Invalid coordinate values" format:@"Latitude: %f Longitude: %f", coordinate.latitude, coordinate.longitude];
}
Upvotes: 0
Reputation: 52575
Your comparison should work. The compiler will convert the integer to a floating point number before comparing. If you want to be sure you can change it to:
if (coordinate.latitude > -90.0 && coordinate.latitude< 90.0)
But your main problem, I think, is that you're reporting the wrong numbers. %d
in a format string tells it to print an integer. You need to tell it to output a floating point number:
[NSException raise:@"Invalid latitude value" format:@"Latitude of %f is invalid", coordinate.latitude];
Upvotes: 2
Reputation: 363848
The problem is not in the comparison.
if (coordinate.latitude > -90 && coordinate.latitude < 90)
[NSException raise:@"Invalid latitude value"
format:@"Latitude of %d is invalid", coordinate.latitude];
should raise an exception for -30.99986 by the logic of this code, since it's between -90 and 90. The problem is the %d
you use to format the error message, which regards its argument as int
and gives you strange results.
Upvotes: 1