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
Fabricio Puerto
2,549 PointsMy InventoryUnarchiver crashes the app, but it's the same as the teacher's note!
Here is the teacher's version:
class InventoryUnarchiver {
class func vendingInventoryFromDictionary(_ dictionary: [String : AnyObject]) throws -> [VendingSelection : ItemType] {
var inventory: [VendingSelection : ItemType] = [:]
for (key, value) in dictionary {
if let itemDict = value as? [String : Double],
let price = itemDict["price"], let quantity = itemDict["quantity"] {
let item = VendingItem(price: price, quantity: quantity)
guard let key = VendingSelection(rawValue: key) else {
throw InventoryError.InvalidKey
}
inventory.updateValue(item, forKey: key)
}
}
return inventory
}
}
Here is my version:
class InventoryUnarchiver {
class func vendingInventoryFromDictionary ( dictionary: [String: AnyObject]) throws -> [ VendingSelection: ItemType] {
var inventory: [VendingSelection: ItemType] = [:]
for (key, value) in dictionary {
if let itemDict = value as? [String: Double],
let price = itemDict["price"], let quantity = itemDict["quantity"] {
let item = VendingItem(price: price, quantity: quantity)
guard let key = VendingSelection(rawValue: key) else {
throw InventoryError.InvalidKey
}
inventory.updateValue(item, forKey: key)
}
}
return inventory
}
}
It looks the same, but for some reason my version crashes the app, and when I just copy the teachers version instead of mine it works. Any idea why???!!
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! I can see at least something that's not 100% the same as the teacher's version. In the teacher's version (at the very beginning) you'll see this:
(_ dictionary: [String : AnyObject])
But yours looks like:
( dictionary: [String: AnyObject])
Note the underscore in the teacher's version preceding the word "dictionary".
Let me know if this helps!
Michael Hulet
47,913 PointsFor what it's worth, this is just a matter of calling semantics. In a newer version of Swift (definitely Swift 3, probably Swift 2, maybe even Swift 1), this would cause a compilation error, not a crash. Judging by the source code, this app is written in at least Swift 2.2
Michael Hulet
47,913 PointsMichael Hulet
47,913 PointsCan you post a copy of the crash log and maybe a stack trace of the crash? What line is it crashing on?