2016-07-13 123 views
0

你好,我是Swift的新手,我在斯坦福大學的iTunes U課程上學習。我正在編程一個計算器。課程視頻中的講師具有相同的代碼,軟件和相同版本的XCode。Value of type ...沒有會員

下面是視圖控制器相關代碼:

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

那麼這有什麼問題?

+0

這將有助於如果您修復縮進,以便可以看到定義的位置rts和結束。 –

+6

你的'{}'和縮進全部搞砸了,但它看起來像你的'result' var不在類定義 – dan

+0

我認爲一旦你在類內移動結果,你也會遇到不匹配類型的問題。 「displayValue」是一個UILabel,「result」是一個Double。我懷疑你可能想要:displayValue.text =「斜槓(brain.result)」....「斜槓」是符號,而不是單詞,但評論似乎並沒有讓我把那個符號放在那裏。 – ghostatron

回答

0

您需要

var result: Double { 
    get { 
     return 0.0 
    } 
} 

搬到這裏來結果的聲明,在類內:所以你的錯誤

class calculatorBrain { 

    var result: Double { 
     get { 
      return 0.0 
     } 
    } 


... 


} 

既然你是CalculatorBrain類以外的定義結果:

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

相關問題