Angela Yu Course Part 3 (up to lesson 59)
I figured it out! So simple - the array string was as "dice 1" rather than "dice1". That explains why the dice would disappear - because the value didn't actually relate to anything! Ah! Phew - it would've been embarrassing to have posted that as a question. OK, so that's a relief. I was also thinking today - while walking around Hampton Court - that I code a Yahtzee game. It would take four dice and a load of labels of the options. I'm going to have a go at that, once the dice app is complete! Angela said that's the best way to learn so I'm revved about that.
The DRY Principle
Now I know what this means - Don't Repeat Yourself.
The 'View Did Load' screen - this is where we want to put in a set of new dice cases! We could copy and paste but DRY - there must be a better way.
So a function would be better for this.
The DRY Principle
Now I know what this means - Don't Repeat Yourself.
The 'View Did Load' screen - this is where we want to put in a set of new dice cases! We could copy and paste but DRY - there must be a better way.
So a function would be better for this.
func updateDiceImages() {
randomDiceIndex1 = Int(arc4random_uniform(6))
randomDiceIndex2 = Int(arc4random_uniform(6))
diceImageViewOne.image = UIImage(named: diceArray[randomDiceIndex1])
diceImageViewTwo.image = UIImage(named: diceArray[randomDiceIndex2])
}
This is VERY clever - having the function contain all the detail, then just using the function.
A good example of how a particular function has many steps. We want to call in one go rather than have to break down the steps every single time!
I'd say that DRY is the one of the main things I need to work on. My switch code was far too complex and long-winded compared to Angela's superior way.
How to Add Motion Detection to Your App
Angela makes the point of using the documentation - using the search bar. E.g. searching for 'shake motion'.
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
updateDiceImages()
}
Not sure where 'override' has come from. Hasn't been explained. Anyway, the shake gesture has been added. That means that when shaking the iPhone, the dice will roll! So it responds to tap and shake.
Challenge #2 Magic Eight Ball
Now to put things into practice!
OK - I used what I did before in the Dicee app to help and it's a good job I did, as the order of where to put everything is pretty specific! E.g. I have to call the function within the button being pressed action function, then declare what the function is! It makes me think it would be easier to do what Pasan did before - create a separate Swift file to put this code into. Also, why the ballArray and the randomNumber variables within the first bit of code? OK the answer is no difference whatsoever! I prefer it there - and as I've just mentioned, even better as a separate Swift file!
Anyway, it's all good!
In this, the 'YES' text changes both on start up, as well as when you click 'ASK'. Same principle as the dice, and one which I'm sure will be very useful!
I suppose the difference in putting the array and randomNumber values within the function means that they become LOCAL not GLOBAL, which could make things tricky if creating more code. As I'm not, it doesn't actually matter.
Cool, so we are done for today! Lots achieved and lots to be proud about as it's got me thinking about other ideas, it's great to see code in practice and rewarding to be independent with it - within Angela's tight framework of course! Tomorrow I'll squeeze in some time for more - Angela says the next part will be more about the language so I'll probably be able to skip through a lot of that.
Comments
Post a Comment