swift - 函数 Swift 中缺少返回值

标签 swift

我收到此错误 Missing return in a function expected to return "User" 此代码:

var winner : User? {

    if player1TotalScore > player2TotalScore {
        return player1

    } else if player1TotalScore == player2TotalScore {
        return nil
    }
}

最奇怪的是我在几行中做了基本相同的事情:

var opponent : User! {
    if player1.isEqual(User.currentUser()) {
        return player2
    } else {
        return player1
    }
}

我在第一段代码中做错了什么?

最佳答案

如果 player1TotalScore 会发生什么小于 player2TotalScore .它同时落在if条件并且没有返回值。你必须处理每一个案例,而你在这里遗漏了一个。这就是为什么 else有效,它同时处理 ==<例。

关于swift - 函数 Swift 中缺少返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33135726/

相关文章:

swift - 无法从通用结构调用通用函数

swift - 如何使用 SwiftUI 将变量传递到 MFMailComposeViewController() 中?

ios - FacebookSDK(4.7) 自定义登录 UI 按钮 - Swift 2.0

ios - 简化字典(xcode 永远保持索引)

json - 如何将 JSON 元素与 alamofire 一起使用

ios - 具有业务和数据层的 Swift MVVM 示例

另一个文件中的 iOS UITextView 委托(delegate)

ios - 自定义 "flatmap"在每个请求之前调用

ios - 该应用程序完美运行 6-8 次点击,之后就崩溃了

ios - 界面生成器中 UIView 的边框颜色不起作用?