Build a Vending Machine App - Part 2
Yes, the momentum continues! Today I'm planning on completing the rest of this course! I'm sure there'll be more new content along the way, which is a good thing! Even though I'll probably stumble along the way, it's the best way to learn. I'm not deliberately writing in platitudes; I just want to make it clear in my head that I don't want to come undone today. Anyway, last time I started this course by writing code involving protocols, enums, classes and structs - a real combination of what I've learned. Here we go!
Type Casting
The first thing Pasan says is that we're moving away from the project to do some code in a playground....good to be mixing this up - not using one entirely and not the other.
So arrays can only contain one type, however in the example I've copied into playground he mentions this is (deliberately) not the case. They have a common base type. Swift converts/casts each object to a base type to make it homogenous.
Downcast to a different type...this is already confusing. Looking through the comments, I'm heartened to see that there is a LOT of confusion over this course - so it's not just me! Also, others have mentioned that the next courses are better.
'is' operator - if something is true/false. OK so it's to see if a value is of a particular type. Not == to - a different thing!
'as' operator - allows you to cast to a specific sub-class (down casting). as? or as! The as? is a conditional. The as! is forced - only use when the downcast WILL succeed!
This is certainly confusing! But I've given it a go.
Any object represents any class in swift. So down casting is used...
Downcasting is converting from a superclass to a more specific subclass.
Creating an Inventory
Pasan makes the point of how useful the protocols are - it gives us more freedom of what we return and it means no subclasses are needed.
Casting the value from each iteration to a more appropriate type...still not sure what that actually means!
Retrieving a value from a dictionary using the key returns an optional value....
This is what I'm trying to deal with - massively confusing!
Finishing Touches
So as a recap here's what we've done:
We've got the value, using the p-list, converting it to a dictionary object. But we want something from more specific types.
We've pulled out the nested dictionary, creating an 'item'.
Now, we need to convert the key from the string gum to the vendingSelection gum. We need a more specific type.
Enumerations contain raw values - the primitive types.
So by typing in a type declaration with the enum, it means that we're assigning a raw value. It gives an init method. We can convert the string from the dictionary to the correct enum member!
OK something else has clicked - having the built-in error handling means if the user does NOT select properly, or follows what we want them to do, that error message will come up - not just for our benefit in the code but so that it doesn't crash the code and brings up an error message to them instead! Ah!
OK, back in the ViewController file....
Right that will do for now - as it's been made clear by not just me but others, this was tricky. Lots of code that was new and old all being used together, plus some concepts that are still unclear. I'm hoping that the Udemy courses on making apps are more straightforward as I'm not sure what I'm really learning from this!
Type Casting
The first thing Pasan says is that we're moving away from the project to do some code in a playground....good to be mixing this up - not using one entirely and not the other.
So arrays can only contain one type, however in the example I've copied into playground he mentions this is (deliberately) not the case. They have a common base type. Swift converts/casts each object to a base type to make it homogenous.
Downcast to a different type...this is already confusing. Looking through the comments, I'm heartened to see that there is a LOT of confusion over this course - so it's not just me! Also, others have mentioned that the next courses are better.
'is' operator - if something is true/false. OK so it's to see if a value is of a particular type. Not == to - a different thing!
'as' operator - allows you to cast to a specific sub-class (down casting). as? or as! The as? is a conditional. The as! is forced - only use when the downcast WILL succeed!
This is certainly confusing! But I've given it a go.
Any object represents any class in swift. So down casting is used...
Downcasting is converting from a superclass to a more specific subclass.
Creating an Inventory
Pasan makes the point of how useful the protocols are - it gives us more freedom of what we return and it means no subclasses are needed.
Casting the value from each iteration to a more appropriate type...still not sure what that actually means!
Retrieving a value from a dictionary using the key returns an optional value....
class InventoryUnarchiver {
static func vendingInventory (fromDictionary dictionary: [String: AnyObject]) -> [VendingSelection: VendingItem] {
var inventory: [VendingSelection: VendingItem] = [:]
for (key, value) in dictionary {
if let itemDictionary = value as? [String: Any], let price = itemDictionary["price"] as? Double, let quantity = itemDictionary["quantity"] as? Int, let item = Item(price: price, quantity: quantity) {
}
}
return inventory
}
Finishing Touches
So as a recap here's what we've done:
We've got the value, using the p-list, converting it to a dictionary object. But we want something from more specific types.
We've pulled out the nested dictionary, creating an 'item'.
Now, we need to convert the key from the string gum to the vendingSelection gum. We need a more specific type.
Enumerations contain raw values - the primitive types.
So by typing in a type declaration with the enum, it means that we're assigning a raw value. It gives an init method. We can convert the string from the dictionary to the correct enum member!
OK something else has clicked - having the built-in error handling means if the user does NOT select properly, or follows what we want them to do, that error message will come up - not just for our benefit in the code but so that it doesn't crash the code and brings up an error message to them instead! Ah!
OK, back in the ViewController file....
let vendingMachine: VendingMachine
required init?(coder aDecoder: NSCoder) {
do {
let dictionary = try PlistConverter.dictionary(fromFile: "VendingInventory", ofType: "plist")
let inventory = try InventoryUnarchiver.vendingInventory(fromDictionary: dictionary)
self.vendingMachine = FoodVendingMachine(inventory: inventory)
} catch let error {
fatalError("\(error)")
}
super.init(coder: aDecoder)
}
More of "you're going to do something but you won't understand".....
Should I look into it myself? Should do really - but when I have, it's always very verbose and/or technical. I hate to say it Pasan, but this course does indeed seem unnecessarily complicated. Hopefully the last couple (which are 'workshops' rather than courses) will be more accessible. I'd hat to finish this Treehouse run on a down note.
Right that will do for now - as it's been made clear by not just me but others, this was tricky. Lots of code that was new and old all being used together, plus some concepts that are still unclear. I'm hoping that the Udemy courses on making apps are more straightforward as I'm not sure what I'm really learning from this!
Comments
Post a Comment