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

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

Rea Rahhal
PLUS
Rea Rahhal
Courses Plus Student 4,732 Points

Use of unresolved identifier FoodVendingMachine. I followed all the steps .don't know what's wrong

let vendingMachine:VendingMachine

required init?(coder aDecoder: NSCoder) {
    do{

        let dictionary = try PListConverter.dictionary(fromFile: "VendingInventory", ofType: "plist")

        let inventory = try InventoryUnarchiver.vendingInventory(fromDictionary: dictionary)
        self.vendingMachine = FoodVendingMachine(inventory:inventory)

    }catch let error{
        fatalError("\(error)")
    }
    super.init(coder: aDecoder)
}
Taylor Smith
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Taylor Smith
iOS Development Techdegree Graduate 14,153 Points

Hi Rea! So this error means it's not seeing your FoodVendingMachine object. This could be because of a typo, or maybe you named it something else by accident. Can you upload you FoodVendingMachine object code so I can see it?

Rea Rahhal
Rea Rahhal
Courses Plus Student 4,732 Points

Here is VendingMachine.swift

import Foundation 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

}

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{

func vend(_ quantity: Int, _ selection: VendingSelection) throws {
}

func deposit(_ amount: Double) {
}

let selection:[VendingSelection]=[.soda, .dietSoda, .chips, .cookie, .sandwich, .candyBar, .popTart, .water,.wrap, .fruitJuice,.sportsDrink, .gum, .wrap]//read only property var inventory:[VendingSelection:VendingItem] var amountDeposited = 10.0 required init(inventory: [VendingSelection : VendingItem]) { self.inventory = inventory } } }