Swift 错误:预期返回“字符串"的函数中缺少返回值 [英] Swift error: Missing return in a function expected to return 'String'

查看:39
本文介绍了Swift 错误:预期返回“字符串"的函数中缺少返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Apple 的 QuestionBot 添加编辑一些代码.我想出了这个:

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

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

}

但是有一个错误:在预期返回字符串"的函数中缺少返回值.我该怎么办,谢谢?

But there's an error: Missing return in a function expected to return 'String'. What should I do, thanks?

推荐答案

预期返回字符串"的函数中缺少返回

Missing return in a function expected to return 'String'

应该函数 return 什么,因为你没有设置 return 如果不匹配任何一个 question.hasPrefix()

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

这篇关于Swift 错误:预期返回“字符串"的函数中缺少返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆