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

Matthew Ingram
PLUS
Matthew Ingram
Courses Plus Student 4,718 Points

it isn't printing the inventory

let vendingMachine: VendingMachine var currentSelection: VendingSelection? var quantity = 1

required init?(coder aDecoder: NSCoder) {
    do {
        let dictionary = try PlistConverter.dictionary(fromFile: "VendingInventory", ofType: "plist")
        let inventory = try InventoryUnarchiver.vendingInvendory(fromDictionary: dictionary)
        self.vendingMachine = FoodVendingMachine(inventory: inventory)
    } catch let error {
        fatalError("\(error)")
    }

    super.init(coder: aDecoder)
}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    setupCollectionViewCells()

print(vendingMachine.inventory)

}

when I got to the console to see the inventory all I see is an empty dictionary [:]

I don't think this is the problem because you would have a compiler error, but let inventory = try InventoryUnarchiver.vendingInvendory(fromDictionary: dictionary) has a typo. vendingInvendory

1 Answer

Hey, I'm not sure if you figured out how to solve this yet, but I was experiencing the same issue. I fixed it by ensuring I used the correct spelling in the if let statement in InventoryUnarchiver function.

if let itemDictionary = value as? [String: Any], let price = itemDictionary["price"] as? Double, let quantity = itemDictionary["quantity"] as? Int{
...
}

Initially I was using itemDictionary["quatity"] instead of itemDictionary["quantity"], it could not find the information needed to satisfy the if let and VendingItem protocol.

NOTE: I used a print statement in the PlistConverter before the return statement to ensure the dictionary is being populated. print(dictionary)

Hope this helps!