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 2.0 Loading Data From a Resource Finishing Touches

Austin Hawks
Austin Hawks
Courses Plus Student 3,003 Points

I'm getting the error cannot call non function type 'module<VendingMachine>'. Please help

let vendingMachine: VendingMachineType
    required init?(coder aDecoder: NSCoder) {
        do {
            let dictionary = try plistCoverter.dictionaryFromFile("VendingInventory", oftype: "plist")
            let inventory = try InventoryUnarchiver.vendingInventoryFromDictionary(dictionary)
            self.vendingMachine = VendingMachine(inventory: inventory)
            //using self outside of a init method ?
        } catch let error { fatalError("\(error)") }

Here is the code and the error is on the self.vendingMachine = VendingMachine(inventory: inventory) line. And here's the protocol vendingMachine is assigned to:

protocol VendingMachineType {
    var selection: [VendingSelection] { get }
    var inventory: [VendingSelection: ItemType] { get set }
    init(inventory: [VendingSelection: ItemType])
    func vend(selection: VendingSelection, quantity: Double) throws
    func deposit(amount: Double)

1 Answer

Hi Austin,

Hopefully you've found the issue by now, but if not, here's my guess.

On the line with the error

self.vendingMachine = VendingMachine(inventory: inventory)

the compiler should call the init method of the VendingMachine class (in VendingMachine.swift). To illustrate we can write it like this:

self.vendingMachine = VendingMachine.init(inventory: inventory)

However, from the error message it seems the compiler does not recognize the VendingMachine class.

To check if this actually is the case you can option-click on VendingMachine on the line

self.vendingMachine = VendingMachine(inventory: inventory)

and then the pop-up should show you something like this:

Declaration    class VendingMachine : VendingMachineType
Declared In    VendingMachine.swift

If the pop-up doesn't show you that, then it's time to double check the VendingMachine class (in VendingMachine.swift) for typos etc.

Hope this helps.