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

Joe Sloan

I'm just starting to learn about Object Oriented, and I've begun writing up 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.

Thanks for any help!

OOPer

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unexpected Non-Void Return Value In Void Function (Swift 2.0)

Unexpected non-void return value in void function (Swift 3)

Swift NumberOfRowsInSection Unexpected non-void return value in void function

Class understanding: Unexpected non-void return value in void function (swift 3)

unexpected non void return value in void function

Unexpected non-void return value in void function

'Unexpected non-void return value in void function' in swiftui

Unexpected non-void return value in void function - Swift 4 (Firebase, Firestore)

Unexpected non-void return value in void function swift 4 using json serialisation

Unexpected non-void return value in void function Swift3

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

why in this code would I be getting "Unexpected non-void return value in void function"

Retrieve product localised price - Unexpected non-void return value in void function

Why does Unexpected non-void return value in void function happen?

swift Non-void function should return a value in a guard let

Return value void * in a function

How to return nil object in non void function - swift 2.1?

Restrict function return value to void

Swift 3: Cannot call value of non-function type '(() -> Void)?'

Is the return command in a non-void function necessary?

Type No return, in function returning non-void

Non-void function potential lack of return

What is the return value if we dont return anything from a non void return typed function in c++?[Experimental]

What's the return value of a non-void function missing return statement?

Warning 'return' with no value, in function returning non-void - What should it return?

void function pointer return value in C

Unclear about return value of a void function in C

Should i return a value for a Future<void> Function?

Why does a void function return a value?