28

I've begun writing a user class that has a method for calculating the user's distance from an object. It looks like this:

class User{
    var searchRadius = Int()
    var favorites : [String] = []
    var photo = String()
    var currentLocation = CLLocation()

    func calculateDistance(location: CLLocation){
        let distance = self.currentLocation.distanceFromLocation(location)*0.000621371
        return distance //Error returns on this line
    }
}

At the line marked above, I get the following error:

(!) Unexpected non-void return value in void function

I've looked elsewhere for a solution, but can't seem to find anything that applies to this instance. I've used the distanceFromLocation code elsewhere, and it's worked okay, so I'm not sure what's different about the usage in this case.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Joe Sloan
  • 775
  • 2
  • 9
  • 16
  • 3
    This could also be because your returning from a completionHandler which aren't suppose to return. See [This answer](https://stackoverflow.com/questions/32121108/unexpected-non-void-return-value-in-void-function-swift-2-0) – mfaani Sep 18 '17 at 18:28

2 Answers2

45

You are missing return type in your method header.

func calculateDistance(location: CLLocation) -> CLLocationDistance {

Seemingly my answer looks as an inferior duplicate, so some addition.

Functions (including methods, in this case) declared without return types are called as void function, because:

func f() {
    //...
}

is equivalent to:

func f() -> Void {
    //...
}

Usually, you cannot return any value from such void functions. But, in Swift, you can return only one value (I'm not sure it can be called as "value"), "void value" represented by ().

func f() {
    //...
    return () //<- No error here.
}

Now, you can understand the meaning of the error message:

unexpected non-void return value in void function

You need to change the return value or else change the return type Void to some other type.

OOPer
  • 47,149
  • 6
  • 107
  • 142
  • 1
    My solution was different for the same debug message. I was trying to return a value within a closure of type `(Data?, URLResponse?, Error?) -> Void`. It was confusing because I was using this closure within a function of signature `() -> Void`. To fix this, I saved the data to a local variable, then returned it outside the closure. – Andrew Kirna Mar 23 '19 at 22:30
2

Your function calculateDistance does not specify a return value. That means it does not return anything.

However, you have a line return distance, which is returning a value.

If you want your function to return a distance, you should declare it like this:

func calculateDistance(location: CLLocation) -> CLLocationDistance
{
  //your code
  return distance
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • 2
    You should accept one of the answers. And, you should probably read the Apple Swift iBook. It covers functions and return values quite clearly. – Duncan C Jul 19 '16 at 22:53