Search code examples
objective-cnsnumberformattercllocationdistance

how can i convert NSNumber to CLLocationDistance?


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.


Solution

  • 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
    }