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 Modeling An Interface Concrete Types and Property Lists

FAKHRI ASLANOV
PLUS
FAKHRI ASLANOV
Courses Plus Student 2,484 Points

I do everything in the code but it does not work. Need help :/

Here's the code. I get an error that says "Assigning a property to itself" when I try to assign self.inventory to inventory.

import Foundation

enum VendingSelection {
    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
}

class FoodVengindMachine: VendingMachine {


    let selection: [VendingSelection] = [.soda, .dietSoda, .chips, .candyBar, .cookie, .fruitJuice, .gum, .popTart, .sandwich, .wrap, .water, .sportsDrink]

    var inventory: [VendingSelection: VendingItem]
    var amountDeposited: Double = 10.0

    required init(inventory: [VendingSelection : VendingItem]) {
        self.inventory = inventory
    }

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

    func deposit(_ amount: Double) {
    }
}