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 trialPema Khando Shachentsang
Courses Plus Student 1,210 PointsVending machine
protocol VendingMachineType { var selection: [VendingSelection] { get} var inventory: [VendingSelection: ItemType] { get } var amountDeposited: Double { get set } init (inventory: [VendingSelection: ItemType]) func vend(selection: VendingSelection, quantity: Double) throws
} protocol ItemType { var price: Double { get} var quantity: Double { get set } }
// Error Types
enum InventoryError: ErrorType { case InvalidResource case ConversionError case InvalidKey }
// Helper Classes
class PlistConverter { class func dictionaryFromFile(resource: String, ofType type: String) throws -> [String : AnyObject] { guard let path = NSBundle.mainBundle().pathForResource(resource, ofType: type) else {
throw InventoryError.InvalidResource
}
guard let dictionary = NSDictionary(contentsOfFile: path),
let castDictionary = dictionary as? [String: AnyObject] else {
throw InventoryError.ConversionError
}
return castDictionary
}
}
class InventoryUnarchiver {
class func vendingInventoryFromDictionary(dictionary: [String : AnyObject]) throws-> [VendingSelection : ItemType] {
var inventory: [VendingSelection : ItemType] = [:]
for (key, value) in dictionary {
if let itemDict = value as? [String : Double],
let price = itemDict["price"], let quantity = itemDict["quantity"] {
let item = VendingItem(price: price, quantity: quantity)
guard let key = VendingSelection( rawValue: key) else { throw InventoryError.InvalidKey
}
inventory.updateValue(item, forKey: key)/ i don't know items is error
}
}
return inventory
}
}
// Concerte Types
enum VendingSelection : String { case Soda case DietSoda case Chips case Cookie case Sandwich case Wrap case CandyBar case PopTart case Water case FruitJuice case SportsDrink case Gum }
struct VendingItem { let price: Double var quantity: Double }
class VendingMachine: VendingMachineType {
let selection: [VendingSelection] = [.Soda,.DietSoda,.Chips,.Cookie
,.Sandwich,.Wrap,.CandyBar,.PopTart,.Water,.FruitJuice,.SportsDrink,.Gum]
var inventory: [VendingSelection: ItemType]
var amountDeposited: Double = 10.0
required init(inventory: [VendingSelection : ItemType]) {
self.inventory = inventory
}
func vend(selection: VendingSelection, quantity: Double) throws {
// add code
}
func deposit(amount: Double) {
// add code
}
}
inventory.updateValue(item, forKey: key) this is a problem. they said Argument type 'VendingItem' does not conform to expected type 'ItemType' how can i fix this problem please help me
1 Answer
Cindy Lea
Courses Plus Student 6,497 PointsYou post all this code & dont tell us what the error is or what you need help with. We are all students & dont have time to look through bulky code without any clues. You can also attach c ode crrectly so its easier to read.