xcode 类型的值 ... 没有成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38358904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:02:29  来源:igfitidea点击:

Value of type ... has no member

iosswiftxcode

提问by Mia Maria

Hello I'm new to Swift and I'm learning it on iTunes U, the Stanford University course. And I'm programming a calculator. The instructor in the course video has the same code, software and same version of XCode.

您好,我是 Swift 的新手,我正在斯坦福大学的 iTunes U 课程中学习它。我正在编写一个计算器。课程视频中的讲师具有相同的代码、软件和相同版本的XCode。

Here's the relevant code for ViewController:

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

The error is in the last sentence: displayValue = brain.resultThis is the error: Value of type 'CalculatorBrain' has no value 'result'

错误在最后一句:displayValue = brain.result这是错误:“CalculatorBrain”类型的值没有值“结果”

This is the CalculatorBrain code:

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

So what's the problem?

所以有什么问题?

采纳答案by Wyetro

You need to move the declaration of result

您需要移动结果的声明

var result: Double { 
    get {
        return 0.0 
    }
}

here, to inside of the class:

在这里,到课堂内部:

class calculatorBrain {

    var result: Double { 
        get {
            return 0.0 
        }
    }


...


}

Since you are defining result outside of the CalculatorBrainclass so you get the error:

由于您是在CalculatorBrain类之外定义结果,因此您会收到错误消息:

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

'CalculatorBrain' 类型的值没有值 'result'