Search code examples
objective-cdoublecllocation

Cast as double?


How can I turn or maybe cast something to a double? CLLocationDegrees is really a double so how do I cast it as double so my if always returns doubles and I can change my findAllLocalCountTotal params to double instead of CLLocationDegrees?

if(self.lastKnownLocation == nil ){
    double lat = [CLController sharedInstance].latitude;
    double lng = [CLController sharedInstance].longitude;

}else{
    CLLocationDegrees lat = self.lastKnownLocation.coordinate.latitude;
    CLLocationDegrees lng = self.lastKnownLocation.coordinate.longitude;
}

NSNumber *tempInt = self.lastPostsGrabbedCounter;



//Make call to get data with lat lng
self.webServiceAllCount = [Post findAllLocalCountTotal:(CLLocationDegrees)lat withLong:(CLLocationDegrees)lng];

Solution

  • Just use a cast and also move the variable declaration outside of the if - the if & else branches are distinct scopes and introduced their own variables:

    double lat, lng;
    if(self.lastKnownLocation == nil)
    {
       lat = [CLController sharedInstance].latitude;
       lng = [CLController sharedInstance].longitude;
    }
    else
    {
       lat = (double)self.lastKnownLocation.coordinate.latitude;
       lng = (double)self.lastKnownLocation.coordinate.longitude;
    }