I have to convert an NSNumber
to CLLocationDistance
for comparing them. So are there any formatter to do that? I searched at internet but i could not find.
Asked
Active
Viewed 678 times
0

rmaddy
- 314,917
- 42
- 532
- 579

user2393702
- 109
- 2
- 8
-
see http://stackoverflow.com/questions/10605620/problems-converting-data-in-cllocationdistance – May 18 '13 at 15:10
-
As far as i understand, in that question person tries to add a CLLocationDistance to his NSArray and answer suggest him to convert a distance to NSNumber. But i am trying to convert a NSNumber to CLLocatinDistance. – user2393702 May 18 '13 at 15:23
1 Answers
1
CLLocationDistance
is really just a double
. All you need is this:
NSNumber *number = ... // some number
CLLocationDistance distance = ... // some distance
if (distance == [number doubleValue]) {
// they are equal
}
Keep in mind that comparing two double
values for equality can appear to fail due to the inexactness of most double
values.
if (abs(distance - [number doubleValue]) <= DBL_EPSILON) {
// they are equal
}

rmaddy
- 314,917
- 42
- 532
- 579
-
Can you compare `floats` or `doubles` using `==`? As far as I know you should subtract them, `abs` the result and compare them to an Epsilon value. ;) – HAS May 18 '13 at 16:22
-
1You can use `==` but as I stated, it may fail in many cases. Your suggestion is exactly the solution to my warning at the end of my answer. – rmaddy May 18 '13 at 16:24