2016-09-05 112 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" 
     }