Swift 错误:预期返回“字符串"的函数中缺少返回值

Swift 错误:预期返回“字符串

问题描述:

我正在尝试从 Apple 的 QuestionBot 添加编辑一些代码.我想出了这个:

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?

预期返回字符串"的函数中缺少返回

Missing return in a function expected to return 'String'

应该函数 return 什么,因为你没有设置 return 如果不匹配任何一个 question.hasPrefix()

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"
        }