2016-09-05 154 views
1

我想添加編輯從蘋果的QuestionBot一些代碼。我想出了這個:Swift錯誤:缺少返回函數預計返回'字符串'

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

} 

但有一個錯誤:缺少返回的函數預計返回'字符串'。我該怎麼辦,謝謝?

+3

想想看:我應該如果這個問題有沒有這三個前綴的函數返回? –

+0

我明白了。謝謝! – Luka

回答

1

Missing return in a function expected to return 'String'

應的功能return的東西,因爲你沒有設置return如果不符合任何一個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" 
     }