Swift错误:预期返回的函数中缺少返回';字符串';
我正在尝试从苹果的QuestionBot添加一些代码。我想到了这个:Swift错误:预期返回的函数中缺少返回';字符串';,swift,Swift,我正在尝试从苹果的QuestionBot添加一些代码。我想到了这个: func responseToQuestion(question: String) -> String { if question.hasPrefix("hello") { return "Hello" } else if question.hasPrefix("where") { return "There" } else if question.hasPref
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"
}
}
但有一个错误:预期返回“String”的函数中缺少return。我该怎么办,谢谢
预期返回“String”的函数中缺少返回
函数是否应该返回
某些内容,因为您没有设置返回
,如果与任何一个问题不匹配。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"
}
想想看:如果问题的三个前缀都没有,函数应该返回什么?我明白了。非常感谢。