2

I'm sure there is an obvious solution to this issue, but I've been trying different things for a while now and I can't figure it out. Basically I have got the distance between two points, and I am trying to convert it from a Double to an Int (to remove all decimal points), however everything I have tried hasn't worked.

This is my code:

let userLocation = CLLocation(latitude: mapView.userLocation.coordinate.latitude, longitude: mapView.userLocation.coordinate.longitude)
let annotationLocation = CLLocation(latitude: latitudePassed, longitude: longitudePassed)

var distance = CLLocationDistance(annotationLocation.distanceFromLocation(userLocation))

if distance > 1000 {
    distance = distance / 1000
    rideAnnotation.subtitle = "\(distance) kilometer(s)"
} else {
    rideAnnotation.subtitle = "\(distance) meters"
}

I've tried converting to a string then an int, converting to an int with toInt() and a few other things, but to no success.

Anyone have any suggestions?

user3746428
  • 11,047
  • 20
  • 81
  • 137

1 Answers1

5

It's possible I'm missing something, but does let distanceInt = Int(distance) not do exactly what you want?

TwoStraws
  • 12,862
  • 3
  • 57
  • 71
  • I tried that and it gave me the error - Cannot assign a value of type 'Int' to a value of '(CLLocationDistance)' – user3746428 Jul 06 '15 at 22:58
  • Could you check to make sure you're running a recent version of Xcode? It works fine in 6.4 and 7.0b2, and I'm wondering whether you're using an older version of Swift. – TwoStraws Jul 06 '15 at 23:01
  • Just updated to 6.4 actually! So that's definitely not the issue unfortunately. – user3746428 Jul 06 '15 at 23:01
  • Ah! Just restarted Xcode and did a clean and that got rid of the error. Typical! Thanks anyway! – user3746428 Jul 06 '15 at 23:04
  • Wait a mo… assign an Int *to* a CLLocationDistance? That sounds like an unusual error message. I would have expected something like "Cannot find an initializer for type Int that accepts an argument list of type (CLLocationDistance)." I don't mean to be rude, but could you check your code, please? – TwoStraws Jul 06 '15 at 23:04