2016-07-13 20 views
0

こんにちは私はSwiftには新しく、スタンフォード大学のコースであるiTunes Uでそれを学んでいます。私は電卓をプログラミングしています。コース動画の講師には、同じコード、ソフトウェア、同じバージョンのXCodeがあります。タイプの値...メンバーはありません

ここでのViewControllerに関連するコードがあります:エラーが最後の文である

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet private weak var display: UILabel! 

    private var displayValue: Double { 

     get { 
      return Double(display.text!)! 

     } 

     set { 
      display.text = String(newValue) 

     } 
    } 

    ... 

    private var brain = calculatorBrain() 

    @IBAction private func performOperation(sender: UIButton) { 
     if userIsInTheMiddleOfTyping { 
      brain.setOperand(displayValue) 
      userIsInTheMiddleOfTyping = false 
     } 

     if let mathematicalSymbol = sender.currentTitle { 
      brain.performOperation(mathematicalSymbol) 
     } 
     displayValue = brain.result 
    } 
} 

displayValue = brain.result これはエラーです:タイプの値「CalculatorBrain」には値が「結果」を持っていない

これは、 CalculatorBrainコード:

import Foundation 

    func multiply(op1: Double, op2: Double) -> Double { 
     return op1 * op2 
    } 

    class calculatorBrain { 
    private var accumulator = 0.0 

    func setOperand(operand: Double) { 
     accumulator = operand 

    } 

    var operations: Dictionary<String,Operation> = [ 
     "π" : Operation.Constant(M_PI), 
     "e" : Operation.Constant(M_E), 
     "√" : Operation.UnaryOperation(sqrt), 
     "cos" : Operation.UnaryOperation(cos), 
     "×" : Operation.BinaryOperation(multiply), 
     "=" : Operation.Equals 


    ] 
    enum Operation { 
     case Constant(Double) 
     case UnaryOperation((Double) -> Double) 
     case BinaryOperation((Double, Double) -> Double) 
     case Equals 
    } 

    func performOperation(symbol: String) { 
     if let operation = operations[symbol] { 
      switch operation { 
      case .Constant(let value): accumulator = value 
      case .UnaryOperation(let function): accumulator = function(accumulator) 
      case .BinaryOperation(let function): 
      case .Equals: break 
      } 

     } 
    } 
} 

struct PendingBinaryOperationInfo { 
    var BinaryFunction: (Double, Double) -> Double 
    var firstOperand: Double 
} 

var result: Double { 
    get { 
     return 0.0 
    } 
} 

問題は何ですか?あなたがエラーを取得するよう

class calculatorBrain { 

    var result: Double { 
     get { 
      return 0.0 
     } 
    } 


... 


} 

あなたがCalculatorBrainクラスの外の結果を定義しているので:

+0

インデントを修正すると、定義がどこにあるのかわかるようになりますrtsと終了。 –

+6

あなたの '{}' sとインデントはすべて乱れていますが、 'result'のように見えます。varはクラス定義の外にあります。 – dan

+0

クラス内で結果を移動すると、タイプのミスマッチにも問題があります。 "displayValue"はUILabelであり、 "result"はDoubleです。私はあなたが望むかもしれないと思う:displayValue.text = "スラッシュ(brain.result)" .... "スラッシュ"は単語ではなくシンボルですが、コメントは私にそのシンボルを置くように見えません。 – ghostatron

答えて

0

あなたはクラスの内部に、

var result: Double { 
    get { 
     return 0.0 
    } 
} 

ここでの結果の宣言を移動する必要があります。

Value of type 'CalculatorBrain' has no value 'result'

関連する問題