Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Vending Machine App in Swift 2.0 Loading Data From a Resource Finishing Touches

Anthony Lafont
Anthony Lafont
17,075 Points

I get a compiler error "type VendingMachine does not conform to protocol vendingMachineType"

I get a compiler error "type VendingMachine does not conform to protocol vendingMachineType" for the "VendingMachine" class. I can't understand why. Does somebody know?

Here is my code (sorry for the layout):

import Foundation

// Protocols

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)

}

protocol ItemType { var price: Double { get } var quantity : Double { get set } }

// Error Types

enum inventoryError: ErrorType{ case InvalidResource case ConversionError case InvalidKey }

// Helper Class

class PlistConveter { 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)
        }
    }
    return inventory
}

}

// Concrete 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 : ItemType { var 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
}

}

4 Answers

The amountDeposited variable in your VendingMachineType protocol should be a Double instead of a [Double]. :)

Parth Patel
Parth Patel
5,951 Points

You should have

" // Concrete 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 } "

and make sure the v in VendingSelection is capitalized if its capitalized where you defined it.

Anthony Lafont
Anthony Lafont
17,075 Points

Thanks for the answer, but it seems it's not the only problem I had with my code. I still have my compiler error! I repost my code with good format

import Foundation

// Protocols

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)
}

protocol ItemType {
    var price: Double { get }
    var quantity : Double { get set }
}

// Error Types

enum InventoryError: ErrorType {
    case InvalidResource
    case ConversionError
    case InvalidKey
}

// Helper Class

class PlistConveter {
    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)
        }
    }
    return inventory
    }
}

// Concrete 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 : ItemType {
    var 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
    }
}
Anthony Lafont
Anthony Lafont
17,075 Points

Thanks a lot Darya! I've been looking for this error for weeks!