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

Cody Adkins
Cody Adkins
7,260 Points

struct not conforming to type protocol

I keep getting an error message in Xcode when I try making my struct type a protocol i made earlier in the video. The error is at about 1:30 minutes into the video. Can someone explain what I should do to fix this?

In the previous video, Pasan switched from adding code to the ItemType to adding code to the VendingMachineType in a way that was easy to miss, especially if you were typing as he was talking. So, if you did what I did, you added code to the wrong area. Make sure that VendingMachineType and ItemType look like this:

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 }

2 Answers

You are making price and quantity Double arrays, not Doubles. Try this:

protocol ItemType {
    var price: Double {get}  //not [Double]
    var quantity: Double {get set} //not [Double]

struct VendingItem: ItemType {
    let price: Double
    var quantity: Double

Swift protocols are like interfaces in Java. If you say a class or struct will conform to a protocol you are saying it will implement whatever the protocol requires. In the video the ItemType protocol has two variables: price and quantity. So any struct that conforms to this protocol must have those two properties, as Pasan's VendingItem struct does (1:26).

Cody Adkins
Cody Adkins
7,260 Points

Here is the protocol I have written, with the struct that is not conforming...

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

struct VendingItem: ItemType { let price: Double var quantity: Double }

I am still getting error...