Building A Vending Machine App - Part 3
Just going to crack on! Not so much happening today so a good chance to. I'm in two minds though - I don't feel like I'm getting that much out of this course, yet. Part of me feels I should go over it, try to make more sense of each step. However, having looked at the comments the general consensus is that this is a HARD course! No point in wasting hours on it. So I've decided to crack on with it, glean what I can; I always have the option to revisit anything if necessary. As I've mentioned before, it will be the Udemy courses of building real-life apps where I will hopefully pick up key skills and useful titbits, key knowledge or however you want to say it. Anyway, here we go....
Diplaying Icons with UIImage
We want to avoid type coupling. So each class has a specific purpose to it. Now we're going to focus on getting the right icons, which match up to what Pasan showed initially.
The icons are already in the assets folder. There are three versions to accommodate different devices.
This whole bit of code doesn't work.
Diplaying Icons with UIImage
We want to avoid type coupling. So each class has a specific purpose to it. Now we're going to focus on getting the right icons, which match up to what Pasan showed initially.
The icons are already in the assets folder. There are three versions to accommodate different devices.
This whole bit of code doesn't work.
func icon() -> UIImage {
if let image = UIImage(named: self.rawValue) {
return image
} else {
return imageLiteral default
}
}
}
Basically the goal here is to have a function that returns the correct image for each item of the vending machine. The space before default should be a question mark/box but doesn't come up for some reason....
return UIImage(named: "default")!
I've had to go back to this for now as the other thing with the box and default caused a myriad of errors (11 came up!).
Modelling the Vending Machine
One of the functions on view controller is part of the 'collectionview' - this is to do with clicking on the icons.
print(vendingMachine.selection[indexPath.row])
This line of code above basically means when each item is chosen, the name of that item comes up in the print section.
But to use the code, we need an object, so a variable is created using the VendingSelection class, then the value for that changes, rather than printing.
There was a whole lot more code - this time an enum for possible errors - in this case why the user could not afford to buy purchases - insufficient funds, out of stock or invalid selection. All that is fine if tricky with the syntax.
Testing Out the Vending Machine
So a lot more code, mostly to do with error handling. Some of it was linking the purchase button to the view controller, with the syntax that still baffles me. There is do, try, catch...all difficult to make sense of. Even Pasan himself seems to indicate that this is a bit of a waste of time! Anyway, I've followed the code, with little understanding of it. To be honest, I just want to get this particular course over and done with now!
Updating the Price
There has been a major issue here - I cannot seem to use the purchase button, even though I've gone through it with a fine tooth comb! Going back through the previous three videos have yielded nothing.
DOH! Don't know how I managed that but just missed the most recent bit of code. Now all is updated!
OK all that now works fine!
I'm getting more familiar with the code and all the different labels at least. The logic is there it's just all so technical and tricky.
if let indexPath = collectionView.indexPathsForSelectedItems?.first {
collectionView.deselectItem(at: indexPath, animated: true)
updateCell(having: indexPath, selected: false)
}
This is to do with the collection views - tell us which cells are selected. So deselecting is used as well. Pasan admits this is 'magic code' and to not worry about understanding it as such. Hmm....!
Working with Steppers
So we're used to buttons - as something to cause an action (function as code). Now we're going to look at steppers! This is presumably adding or minusing. This is genuenyl exciting! It is a great way of selecting e.g. speed that the car is going....
On all of the options, this time we're going to have sender rather than any sender. It has to be more specific. So we know a stepper sent this....
Updating the Quantity
So all of these are more adding objects to the ViewController (good to have more practice with this!) and tweaking the code so values are reset etc.
OK another issue - something keeps causing it to crash and I've tried everything. Must be something not compatible with Xcode!
Lots more confusing stuff - have ploughed on anyway but all quite confusing!
And that will do. Exhausted! Do not want to look at any more of this today! So I'm calling it a day. Confusing stuff but just a few more videos and this damned course is over!
Comments
Post a Comment