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

Optionals and unwrapping

I made a one of my one programs while following along in the course and I ran across unwrapping earlier. I had to use it in a situation where I was appending a dictionary. The unwrapping happens under fun additem in Class "Table". My question is, is this appropriate in this situation?? (I saw the other discussion topic but it didn't answer my question)

MY CODE

/Enter order - a list of items //Total that list of items //Add tax //Print out list of items and total

var menu: [String:Float] = ["Burger":5.0,"Fries":2.0,"Shake":4.0]
var taxRate: Float = 0.0972

class Table { //Declare Vars var tableArray: [Float] = []

//Functions
//Adding items to specific tables ticket 

func addItem(item: String) -> String {
    tableArray.append(menu[item]!)
    return item
}

//Print Ticket

func print() -> (printer: Float, taxs: Float) {
    var total: Float = 0
    for count in tableArray.startIndex..<tableArray.endIndex{
        total += tableArray[count]
    }
    total = total + (total * taxRate)
    return (total,taxRate)

}

}

class Store {

//Declare Vars
//************

//Functions
//*********

func appendMenu (item item: String, price: Float) -> String {
    menu.updateValue(price, forKey: item)
    return item
}

//Adjusting the Tax Rate

func adjustTax(taxrate: Float) {
    taxRate = taxrate
}

}

let store1 = Store() let table1 = Table() let oddCouple = Table() let theNextCouple = Table()

//TESTING ***************** //*************************

oddCouple.addItem("Burger")

table1.addItem("Burger") table1.addItem("Fries") table1.addItem("Shake")

theNextCouple.addItem("Burger") theNextCouple.addItem("Burger") theNextCouple.addItem("Burger")

theNextCouple.print()

store1.appendMenu(item: "Mush", price: 2.75) store1.appendMenu(item: "More", price: 7.0)

wow. I don't know if this is just my browser but the website seemingly butchered my code. Its was a lot more elegant lol.

1 Answer

Re how your code looks, make sure you leave a blank line both before the 3 accent marks and after the 3 accent marks that precede and follow your code. Then it will look fine.

Re your question. The key thing is that if you forcibly unwrap like this, you have to be 100% sure that you'll never try to add an item that not in the menu.

table1.addItem("Burger")
table1.addItem("Fries")
table1.addItem("Shake")
table1.addItem("Coke")  //causes an EXC_BAD_INSTRUCTION error

It's usually better to use something like optional binding if there's any possibility at all of a non-menu item getting passed in.