Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![James Hall](https://secure.gravatar.com/avatar/23a1db6adb0c2bb1a8c9aea25b0c0d53?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
James Hall
2,618 PointsinvalidSelection error: cant't find the problem.
I have copied all of my code from VendingMachine.swift. I keep getting an invalidSelection error and I can not seem to figure out what is causing the issue.
import Foundation
enum VendingSelection: String { case soda case dietSoda case chips case cookie case sandwhich case wrap case candyBar case popTart case water case fruitJuice case sportsDrink case gum }
protocol VendingItem { var price: Double { get } var quantity: Int { get set } }
protocol VendingMachine { var selection: [VendingSelection] { get } var inventory: [VendingSelection: VendingItem] { get set } var amountDeposited: Double { get set }
init(inventory: [VendingSelection: VendingItem])
func vend(_ quantity: Int, _ selection: VendingSelection) throws
func deposit(_ amount: Double)
}
struct Item: VendingItem { let price: Double var quantity: Int }
enum InventoryError: Error { case invalidResource case conversionFailure case invalidSelection }
class PlistConverter { static func dictionary(fromFile name: String, ofType type: String) throws -> [String: AnyObject] { guard let path = Bundle.main.path(forResource: name, ofType: type) else { throw InventoryError.invalidResource }
guard let dictionary = NSDictionary(contentsOfFile: path) as? [String: AnyObject] else {
throw InventoryError.conversionFailure
}
return dictionary
}
}
class InventoryUnarchiver {
static func vendingInventory(fromDictionary dictionary: [String: AnyObject]) throws -> [VendingSelection: VendingItem] {
var inventory: [VendingSelection:VendingItem] = [:]
for (key, value) in dictionary{
if let itemDictionary = value as? [String: Any], let price = itemDictionary["price"] as? Double, let quantity = itemDictionary["quantity"] as? Int{
let item = Item(price : price, quantity : quantity)
guard let selection = VendingSelection(rawValue: key) else{
throw InventoryError.invalidSelection
}
inventory.updateValue(item, forKey: selection)
}
}
return inventory
}
}
class FoodVendingMachine: VendingMachine {
let selection: [VendingSelection] = [.soda, .dietSoda, .cookie, .sandwhich, .candyBar, .popTart, .water, .fruitJuice, .sportsDrink, .gum]
var inventory: [VendingSelection : VendingItem]
var amountDeposited: Double = 10.00
required init(inventory: [VendingSelection : VendingItem]) {
self.inventory = inventory
}
func vend(_ quantity: Int, _ selection: VendingSelection) throws {
}
func deposit(_ amount: Double) {
}
}