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 Inventory Errors

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

I am using swift 4 and Xcode 9 . I have imported the foundation framework however I am still getting an error

Argument labels '(contentsofFile:)' do not match any available overloads Can you please help me?

Here is my code import Foundation enum VendingSelection{//enum to encapsulate various selection options that we have 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}//we cannot change the price var quantity: Int{get set}//needs to be updated } protocol VendingMachine{ var selection:[VendingSelection]{get}//a vending machine needs to have a selection of items to vend

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{//this protocol allows us to expose to the vending machine the information we care ///about: price and qty let price: Double var quantity: Int } enum InventoryError:Error{ case invalidResource case conversionFailure } class PListConverter{//were taking and input and outputting another type we will use a class 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)else{
        throw InventoryError.conversionFailure
    }
}

}

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 } }

1 Answer

Magnus Hållberg
Magnus Hållberg
17,232 Points

Could it be that "contentsOfFile" should be spelled with a capital O in Of?

Rea Rahhal
Rea Rahhal
Courses Plus Student 4,732 Points

I Get a new error :Initializer for conditional binding must have Optional type, not '[String : AnyObject]'