Swift类型不符合协议

Swift类型不符合协议,swift,protocols,Swift,Protocols,该错误发生在类VendingMachine:VendingMachineType{} 我似乎找不到导致问题的差异。我是错过了什么,还是我找错地方了 如果我正确理解了协议,它基本上决定了类要遵循的蓝图,就我所看到的而言,我找不到函数没有遵循协议设置的蓝图的地方 协议: protocol VendingMachineType { var selection: [VendingSelection] { get } var inventory: [VendingSelection: It

该错误发生在类
VendingMachine:VendingMachineType{}

我似乎找不到导致问题的差异。我是错过了什么,还是我找错地方了

如果我正确理解了协议,它基本上决定了类要遵循的蓝图,就我所看到的而言,我找不到函数没有遵循协议设置的蓝图的地方

协议:

protocol VendingMachineType {
    var selection: [VendingSelection] { get }
    var inventory: [VendingSelection: ItemType] { get set }
    var amountDeposited: Double { get set }

    init(inventory: [VendingSelection: ItemType])
    func vend(selection: VendingSelection, quantity: Double) throws
    func deposit(amount: Double)
    func itemForCurrentSelection(selection: VendingSelection) -> ItemType?
}
类别:

class VendingMachine: VendingMachineType {
    let selection: [VendingSelection] = [.Soda, .DietSoda, .Chips, .Cookie, .Sandwich, .Wrap, .CandyBar, .PopTart, .Water, .FruitJuice, .SportsDrink, .Gum]

    var inventory: [VendingSelection: ItemType]
    var amountDesposited: Double = 10.0

    required init(inventory: [VendingSelection : ItemType]) {
        self.inventory = inventory
    }

    func vend(selection: VendingSelection, quantity: Double) throws {
        guard var item = inventory[selection] else {
            throw VendingMachineError.InvalidSelection
        }

        guard item.quantity > 0 else {
            throw VendingMachineError.OutOfStock
        }
        //at this point we have an item and a quantity implement a cancel button as homework here. 

        //time to reduce quantity by amount purchased

        item.quantity -= quantity
        inventory.updateValue(item, forKey: selection)


        //here we are checking to see if we have enough money and throwing an error if they do not
        let totalPrice = item.price * quantity
        if amountDesposited >= totalPrice {
            amountDesposited -= totalPrice
        } else {
            let amountRequired = totalPrice - amountDesposited
            throw VendingMachineError.InsufficientFunds(required: amountRequired)
        }
    }

    func itemForCurrentSelection(selection: VendingSelection) -> ItemType? {
        return inventory[selection]
    }

    func deposit(amount: Double) {
        amountDesposited += amount
    }
}

任何正确方向的帮助或指点都将不胜感激

我将您的代码复制到IBM Sandbox中,并且由于您没有粘贴所有代码,所以不得不进行一些简化。尽管如此,我还是犯了同样的第一个错误。第二个错误是问题的原因:

protocol requires property 'amountDeposited' with type 'Double'

您在类定义中拼错了属性。

我将您的代码复制到IBM Sandbox中,由于您没有粘贴所有代码,因此不得不进行一些简化。尽管如此,我还是犯了同样的第一个错误。第二个错误是问题的原因:

protocol requires property 'amountDeposited' with type 'Double'

您在类定义中拼错了属性。

您的
自动售货机
存储的
数量是一个存储属性。但协议只能声明计算属性

您的
自动售货机
存款金额
是一个存储属性。但协议只能声明计算属性

请用错误的详细信息更新您的问题。创建变量时,我将单词“存款”拼错为“Desposit”。如果查看第三个变量,可以在协议中看到这一点。更正此错误后,构建已成功。谢谢大家的帮助,非常感谢。请更新您的问题,并提供有关错误的详细信息。创建变量时,我将“存款”一词拼错为“Desposit”。如果查看第三个变量,可以在协议中看到这一点。更正此错误后,构建已成功。谢谢大家的帮助,非常感谢。这很尴尬,我在创建变量时把“存款”拼写为“Deposit”。谢谢您的帮助。这太尴尬了,我在创建变量时将“存款”拼写为“Desposit”。谢谢你的帮助。