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 trialEthan Neff
27,058 Pointswhy create two classes?
so we access the two classes via:
let dictionary = try PlistConverter.dictionaryFromFile("VendingInventory", ofType: "plist")
let inventory = try InventoryUnarchiver.vendingInventoryFromDictionary(dictionary)
// helper classes
class PlistConverter {
class func dictionaryFromFile(resource: String, ofType type:String) throws -> [String: AnyObject] {
guard let path = NSBundle.mainBundle().pathForResource(resource, ofType: type) else {
throw InventoryError.InvalidResource
}
guard let dictionary = NSDictionary(contentsOfFile: path), let castDictionary = dictionary as? [String: AnyObject] else {
throw InventoryError.ConversionError
}
return castDictionary
}
}
class InventoryUnarchiver {
class func vendingInventoryFromDictionary(dictionary: [String: AnyObject]) throws -> [VendingSelection: ItemType] {
var inventory: [VendingSelection: ItemType] = [:]
for (key, val) in dictionary {
if let itemDict = val 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
}
}
why wouldn't it be better to access both class methods from the same class?
let dictionary = try VendingData.dictionaryFromPlist()
let inventory = try VendingData.vendingInventoryFromDictionary()
1 Answer
John W
21,558 PointsHi Ethan,
My guess is you were wondering why you wouldn't want to implement all the methods under the same class.
It's just a good practice to have independent concepts implemented separately as different classes, structs, in different source files or even frameworks. Not just for reusing codes, but also to mentally compartmentalize units that build up your app. For example, reading and converting a plist file into a dictionary is not specific to InventoryUnarchiver, it could easily be part of FoobarUnarchiver. By separating the implementation of reading a plist from creating an inventory, it also helps you write better code that is easier to read and more testable.
With that said, the errors being thrown by the PListConverter would've been even better if they were of separate ErrorType instead, e.g. PListConverterError instead of InventoryError.