Swift error: Missing return in a function expected to return 'String'

后端 未结 1 1450
無奈伤痛
無奈伤痛 2020-12-04 03:49

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

func responseToQuestion(question: String) -> String {

    if question.h         


        
相关标签:
1条回答
  • 2020-12-04 04:11

    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"
            }
    
    0 讨论(0)
提交回复
热议问题