i have distance in a variable of type CLLocationDistance i need to convert it in a integer variable how can i do it
i have use
CLLocationDistance kilometers;
int distance = [kilometers intValue];
but its giving error.
help guys
https://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html
distanceFromLocation:
Returns the distance (in meters) from the receiver’s location to the specified location.
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
Parameters
location The other location.
Return Value The distance (in meters) between the two locations.
return type is double
not int
. And its not an NSNumber
.
As of iOS 10, there's a new Measurement
class which makes converting distances a snap.
Here's an extension to convert distances:
extension Double {
func convert(from originalUnit: UnitLength, to convertedUnit: UnitLength) -> Double {
return Measurement(value: self, unit: originalUnit).converted(to: convertedUnit).value
}
}
Example usage:
let miles = 1.0
let metersInAMile = miles.convert(from: .miles, to: .meters)
let mileInFeet = 5280.0
let feetInAMile = mileInFeet.convert(from: .feet, to: .miles)
let marathonInFeet = 138_435.0
let milesInMarathon = marathonInFeet.convert(from: .feet, to: .miles)
let kmInMarathon = marathonInFeet.convert(from: .feet, to: .kilometers)
let inch = 1.0
let centimetersInInch = inch.convert(from: .inches, to: .centimeters)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With