您好我是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'类型的值没有值'result'
这是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 :(得分:0)
您需要移动结果声明
var result: Double {
get {
return 0.0
}
}
这里,在课堂内部:
class calculatorBrain {
var result: Double {
get {
return 0.0
}
}
...
}
由于您在CalculatorBrain
类之外定义结果,因此您收到错误:
类型值&#39; CalculatorBrain&#39;没有价值&#39;结果&#39;