7

Can somebody please help me convert the following Objective-C statement to Swift?

CLLocationDistance distance = [fromLocation distanceFromLocation:toLocation];

I know that it must be simple to do that. I am brand new to iOS programming and any help will be greatly appreciated.

Thank you! :-)

Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47
Proteus
  • 93
  • 1
  • 4
  • 1
    Use Playground to figure it out. The documentation shows you exactly what this call should look like. – Mundi Aug 12 '14 at 18:19
  • 2
    Use the documentation: `func distanceFromLocation(location: CLLocation!) -> CLLocationDistance`. In Xcode Command click on `CLLocationDistance` to get to the documentation and then search for `distanceFromLocation`. – zaph Aug 12 '14 at 18:27

2 Answers2

21
let distance = fromLocation.distanceFromLocation(toLocation)

New syntax:

let distance = aLocation.distance(from: anotherLocation)
Mundi
  • 79,884
  • 17
  • 117
  • 140
4
func distanceFromLocation(_ location: CLLocation!) -> CLLocationDistance

Returns the distance (in meters) from the receiver’s location to the specified location.

Read more here https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocation_Class/index.html#//apple_ref/occ/instm/CLLocation/distanceFromLocation:

Garrett O'Grady
  • 536
  • 5
  • 9