Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 285
Joe Sloan Avatar asked Jul 19 '16 22:07

Joe Sloan


People also ask

Why void function does not return a value?

Return from void functions in C++ The void functions are called void because they do not return anything. “A void function cannot return anything” this statement is not always true. From a void function, we cannot return any values, but we can return something other than values.

Why are functions returning non-void assignable to function returning void?

TypeScript FAQ: Why are functions returning non- void assignable to function returning void ? Because returning something when nothing is expected does no harm, but returning the wrong thing when something is expected does harm.

What is a non-void return type?

A non-void method's return line can be just about anything as long the code after the return matches the return type. Here are some examples. A method can return a literal value. return 500; return 98.6; return "John Wayne"; return true; A method can return the contents of a variable.

Can you return a value in a void method?

Within the body of the method, you use the return statement to return the value. Any method declared void doesn't return a value.


2 Answers

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.

like image 154
OOPer Avatar answered Sep 20 '22 12:09

OOPer


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 } 
like image 41
Duncan C Avatar answered Sep 19 '22 12:09

Duncan C