2017-06-26 11 views
0

の1行を変更したときに、私は復帰をしないのですエラーメッセージを取得していますなぜこの行は私にすべてのエラーを与えるものではありません説明していただけます。誰かが私はコード

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

しかし、この行は私に与えませんエラー:

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

メッセージは次のとおりです(別名「(結果:オプション '(:ダブル?, isPending:ブール値、説明:文字列、ErrorMessageの?文字列結果)' を返すように期待される機能で 欠落リターン、isPending:Bool、description:String、errorMessage:オプション) ')

+0

return文を必要とする!.digit 「どちらもゼロですか? – harald

答えて

1

私は2番目のコードのように、両方のリターンを "IF"内に入れていると思います。それは、どちらも真でない状況があると想定しているため、まったくリターンしません。

0

このエラーメッセージが表示されるのは、プログラムフローのためです。

最初のケースでは、常にpendingBinaryOperation != nil && accumulator != nilの値に基づいて何かが返されます。

しかしpendingBinaryOperation != nil && accumulator != nilともaccumulator!.digit != nilである、そのようなプログラムの流れと第二の場合には、あなたのコードは、それがこのようなエラーメッセージを与え返すために何かを打つことはできません。

0

2番目のコードには未解決の条件がいくつかあります。

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

これはあなたのコードです。さんはaccumulatorまず、第二pendingBinaryOperationのための可能なシナリオを見てみましょう:

  • accumulator != nilpendingBinaryOperator != nil。最初の句が実行されます。
  • accumulator != nil,pendingBinaryOperator == nil。 2番目の句が実行されます。
  • accumulator == nil,pendingBinaryOperator != nil || pendingBinaryOperator !== nil。返品はありません。

これは返品のない2つのシナリオです。

0

エラーは、2番目の式が網羅的ではないために発生します。

第1条件と第2条件の両方がfalseと評価された場合、戻り値はありません。それがエラーメッセージの状態です。

あなたは余分なelse

if .... { 
    return ... 
} else if ... { 
    return ... 
} else { 
    return ... 
} 

たり、とき `pendingBinaryOperation`と`アキュムレータif文を入力した場合、分岐が実行された最後

if .... { 
    return ... 
} else if ... { 
    return ... 
} 
return ... 
関連する問題