iOS Build a Vending Machine App in Swift Using the Vending Machine Modeling the Vending Machine

Huseyn Guliyev
Huseyn Guliyev
2,531 Points

Error in vend function

Cannot subscript a value of type '[VendingSelection : VendingItem]' with an argument of type '[VendingSelection]'

Here is my code:

class FoodVendingMachine: VendingMachine {

let selection: [VendingSelection] = [.soda, .dietSoda, .chips, .cookie, .sandwich, .wrap, .candyBar, .popTart, .water, .fruitJuice, .sportsDrink, .gum]

var inventory: [VendingSelection : VendingItem] 

var amountDeposited: Double = 10.0

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

func vend( _ quantity: Int, _ slection: VendingSelection) throws {
    guard var item = inventory[selection] else {
        throw VendingMachineError.invalidSelection
    }
}

func deposit(_ amount: Double) {
}

}

1 Answer

Johnny Nguyen
Johnny Nguyen
3,875 Points

Look at your function vend():

Do you name your local argument correctly? It should be "selection" instead of "slection".

Huseyn Guliyev
Huseyn Guliyev
2,531 Points

I already found my mistake but thanks anyways!