unexpected non-void return value in void function in new Swift class unexpected non-void return value in void function in new Swift class ios ios

unexpected non-void return value in void function in new Swift class


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.


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}


matomo