首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >请有人解释一下,为什么当我更改一行代码时,我会收到一条错误信息,说我丢失了一条返回。

请有人解释一下,为什么当我更改一行代码时,我会收到一条错误信息,说我丢失了一条返回。
EN

Stack Overflow用户
提问于 2017-06-26 14:58:07
回答 4查看 69关注 0票数 0

这一行不会给我任何错误:

代码语言:javascript
复制
if pendingBinaryOperation != nil && accumulator != nil {
    return (accumulator!.digit, true, pendingBinaryOperation!.description(pendingBinaryOperation!.descriptionOperand, accumulator?.literalDescription ?? " "), accumulator?.errorMessage)
} else  {
    return (accumulator!.digit, false, calculationDescription ?? "", accumulator?.errorMessage)
}

但这一行确实给了我一个错误:

代码语言:javascript
复制
if pendingBinaryOperation != nil && accumulator != nil {
    return (accumulator!.digit, true, pendingBinaryOperation!.description(pendingBinaryOperation!.descriptionOperand, accumulator?.literalDescription ?? " "), accumulator?.errorMessage)
} else if accumulator!.digit != nil {
    return (accumulator!.digit, false, calculationDescription ?? "", accumulator?.errorMessage)
}

消息是:在期望返回的函数中缺少返回(结果: Double?,isPending: Bool,description: String,errorMessage: String?)(又名‘(结果:可选,isPending: Bool,description: String,errorMessage:可选)

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-06-26 15:00:59

我认为,正如在第二段代码中一样,您将两个返回都封装在一个"IF“中,它假设可能存在这样一种情况,即它们都不是真的,因此您将根本没有任何返回。

票数 1
EN

Stack Overflow用户

发布于 2017-06-26 15:13:32

在我看来,得到这个错误消息的唯一可能原因是因为您的程序流。

在第一种情况下,它总是根据这个pendingBinaryOperation != nil && accumulator != nil的值返回一些东西。

但是,在第二种情况下,在这样的程序流中,pendingBinaryOperation != nil && accumulator != nilFalse,而accumulator!.digit != nilFalse,您的代码无法命中要返回的东西,因此它提供了这样一个错误消息

票数 0
EN

Stack Overflow用户

发布于 2017-06-26 15:21:23

在第二段代码中有一些未解决的条件。

代码语言:javascript
复制
if pendingBinaryOperation != nil && accumulator != nil {
    return (accumulator!.digit, true, pendingBinaryOperation!.description(pendingBinaryOperation!.descriptionOperand, accumulator?.literalDescription ?? " "), accumulator?.errorMessage)
} else if accumulator!.digit != nil {
    return (accumulator!.digit, false, calculationDescription ?? "", accumulator?.errorMessage)
}

这是你的密码。让我们先来看看accumulatorpendingBinaryOperation的可能场景:

  • accumulator != nilpendingBinaryOperator != nil.执行第一个子句。
  • accumulator != nilpendingBinaryOperator == nil.执行第二子句。
  • accumulator == nilpendingBinaryOperator != nil || pendingBinaryOperator !== nil.不提供退货。

这是两个没有返回的场景。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44762618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档