1

I'm trying to add edit some code from Apple's QuestionBot. I came up with this:

func responseToQuestion(question: String) -> String {

    if question.hasPrefix("hello") {
        return "Hello"
    } else if question.hasPrefix("where") {
        return "There"
    } else if question.hasPrefix("what"){
        return "I don't know"
    }

}

But there's an error: Missing return in a function expected to return 'String'. What should I do, thanks?

2
  • 8
    Think about it: what should the function return if the question has none of the three prefixes? Commented Sep 5, 2016 at 18:07
  • I see. Thank you! Commented Sep 5, 2016 at 18:09

1 Answer 1

3

Missing return in a function expected to return 'String'

should the function return something , because you did not set return if do not match any one question.hasPrefix()

 func responseToQuestion(question: String) -> String {

            if question.hasPrefix("hello") {
                return "Hello"
            } else if question.hasPrefix("where") {
                return "There"
            } else if question.hasPrefix("what"){
                return "I don't know"
            }
          return "something"
        }
Sign up to request clarification or add additional context in comments.

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.