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

后端 未结 1 1476
無奈伤痛
無奈伤痛 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)
提交回复
热议问题