2016-07-13 120 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'

相关问题