1

I'm getting this error Missing return in a function expected to return "User" for this code:

var winner : User? {

    if player1TotalScore > player2TotalScore {
        return player1

    } else if player1TotalScore == player2TotalScore {
        return nil
    }
}

The most curious thing about this is that I do basically the same thing a few lines up:

var opponent : User! {
    if player1.isEqual(User.currentUser()) {
        return player2
    } else {
        return player1
    }
}

What am I doing wrong in the first bit of code?

3 Answers 3

4

What happens if player1TotalScore is less than player2TotalScore. It falls through both if conditions and has no return value. You have to handle every case, and you're missing one here. That's why the else works, it handles both the == and the < cases.

Sign up to request clarification or add additional context in comments.

Comments

0

In case player1TotalScore < player2TotalScore your code will not return any value.

Comments

0

You miss the last else statement

var winner : User? {

    if player1TotalScore > player2TotalScore {
        return player1

    } else if player1TotalScore == player2TotalScore {
        return nil
    } else {
        /// return player2 ?
    }
}

Without the last else winner returns nothing

Hope this helps you !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.