Angela Yu Course Part 6 (up to lesson 84)
Yes - the first time not in the evening for a short while that I've been able to code. Here is my plan for the next couple of days:
Complete Section 8 - Xylophone app!
Make my own 'Yahtzee' app - this could take some time...
Complete Section 9 - MVC for make a quiz app
This will be plenty to sink my teeth into. Ok here goes!
Introduction
The aim is not so much about creating an app but actually using tools to get help - whether it's using the Swift documentation or other searches. There will be unfamiliar language etc. The purpose is to be more self-reliant - this is something I DEFINITELY need! When I need a certain functionality - finding out for myself. This will be key! There's no point just copying and pasting someone else's code without understanding it; it's much better to find things out and then apply them. Anyway, here goes!
Using Tags
Linking all of the buttons to one action....interesting! So they all trigger the same action.
Using the tag property. Each button has a different tag value - from 1 to 7. Ok!
So presumably this will mean that when clicking on each xylophone button, the tag number will print to the console. Yes that is what happens!
If statements can then be used for this too.
Using the tag property is really useful - creating SEVEN separate buttons with separate outputs would be madness! This helps with the idea for my Yahtzee app!
Stack Overflow
This is basically a q & a website where people can ask questions and answers can be posted - by anyone. As expected, some answers are better than others - when the box is highlighted in green, it means the person who has posted the question has explicitly selected an answer which has been given. All logical.
Providing code snippets...
Importing SpriteKit - used for games, as is GameScene etc.
Good, Angela mentions that optionals are part of Intermediate Swift - they're always something I've found really tricky!
So here is what we have from Stack Overflow:
Incorporate AVFoundation
Complete Section 8 - Xylophone app!
Make my own 'Yahtzee' app - this could take some time...
Complete Section 9 - MVC for make a quiz app
This will be plenty to sink my teeth into. Ok here goes!
Introduction
The aim is not so much about creating an app but actually using tools to get help - whether it's using the Swift documentation or other searches. There will be unfamiliar language etc. The purpose is to be more self-reliant - this is something I DEFINITELY need! When I need a certain functionality - finding out for myself. This will be key! There's no point just copying and pasting someone else's code without understanding it; it's much better to find things out and then apply them. Anyway, here goes!
Using Tags
Linking all of the buttons to one action....interesting! So they all trigger the same action.
Using the tag property. Each button has a different tag value - from 1 to 7. Ok!
@IBAction func notePressed(_ sender: UIButton) {
print(sender.tag)
}
If statements can then be used for this too.
Using the tag property is really useful - creating SEVEN separate buttons with separate outputs would be madness! This helps with the idea for my Yahtzee app!
Stack Overflow
This is basically a q & a website where people can ask questions and answers can be posted - by anyone. As expected, some answers are better than others - when the box is highlighted in green, it means the person who has posted the question has explicitly selected an answer which has been given. All logical.
Providing code snippets...
Importing SpriteKit - used for games, as is GameScene etc.
Good, Angela mentions that optionals are part of Intermediate Swift - they're always something I've found really tricky!
So here is what we have from Stack Overflow:
let url = Bundle.main.url(forResource: "note1", withExtension: "wav")!
do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error as NSError {
print(error.description)
}
}
Now I know about do and catch - it's to do with error handling. That's something else I've always found tricky!
Cool - it works! Well, each button is playing note1, which makes sense considering what we have coded. It also makes total sense to use Stack Overflow - what a great resource for getting bits of code that you need, which you can then understand the logic behind when using them. On Stack Overview, there are other comments which are helpful.
Basically, try out the solution. If that doesn't work, try another answer and/or check the comments from people for useful tips.
I've just added SO to my bookmarks and created an account!
Apple Developer Forums
So similar to SO, someone has asked a question and someone from Apple can help the person basically.
If you still can't find an answer, based on the above or SO, then you can always ask your own question!
Apple Documentation
You can access this online or as the eBook. Either way, you can search and find something though this is tricky to navigate. There is documentation and sample code. Angela doesn't seem to recommend this. It's better to find the code on SO or ADF, then find out about it by clicking on the option button for that particular class/object etc.
API reference -
*This one doesn't seem to work like Angela has shown - seems to have changed!
Incorporate AVFoundation
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioPlayer: AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func notePressed(_ sender: UIButton) {
let soundURL = Bundle.main.url(forResource: "note1", withExtension: "wav")
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
}
catch {
print(error)
}
audioPlayer.play()
}
}
Here is the code! A good point Angela makes is that how can you remember all of this?! Well in this case, you can't, or at least don't need to! It's good to know you have a reference for all of this.
Programs like to collect together 'recipes' - step by step ideas that can be applied to do different things. So if I need to use sound again, I know where to go etc.
Error Handing - Do, Catch and Try
'Throws' - this means it CAN THROW AN ERROR! Well that makes more sense than Pasan did - no offence, Pasan!
Do - what should happen
Catch - what should happen if there is an error (usually print to the debug console)
This looks really useful - for looking up errors! You can copy and paste the error codes in to help understand the error better. Cool!
So what if you were certain that there would be NO error? You can also force it to run, using the !. It means you are 100% sure that there will never be an error. It's called a manual override. Risky!
When to use Error Handling? My understanding is that - according to the documentation with the command button pressed - the 'throws' keyword meant that error handling was basically needed. I think so at least!
YES! I figured out how to solve the issue of the array not matching up with the index value to the note - you need to minus 1. Well that seems to work anyway!
You could change the tag value, but the system gives everything a tag value of 0. That means that it would confuse things with Xcode. Good, didn't do that!
That feels really good - after failing miserably at the last two for-in challenges yesterday, I feel genuinely proud of realising that myself. It was not so obvious but I made it work!
Scope
There's an important point about scope - where to create variables. Before I had a variable inside the action function, but I needed it declared earlier, then updated before the function. So it's a tricky concept this one - still not completely sure when and where to put variables and values etc.
Yes the whole code does work!
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioPlayer: AVAudioPlayer!
var selectedSound = ""
let soundArray = ["note1", "note2", "note3", "note4", "note5", "note6", "note7"]
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func notePressed(_ sender: UIButton) {
selectedSound = soundArray[sender.tag - 1]
playSound()
}
func playSound() {
let soundURL = Bundle.main.url(forResource: selectedSound, withExtension: "wav")
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
}
catch {
print(error)
}
audioPlayer.play()
}
}
It's like she's a mind reader! Next time, we're looking at scope! :)
OK, so putting the selectedSound variable outside of the functions is crucial - it means it can be accessed ANYWHERE.
If I did create the variable within the function then it would be a LOCAL variable. So to use it in multiple functions, which is what you need, it needs to be created within the view controller class.
LOCAL VARIABLE:
Using the apple tree analogy!
Angela tweaks the code so that local variables are used - within the parameters/input of the actual function:
It's basically better to do this than to have a separate global variable with the value, in case that value gets accessed elsewhere!
Well, once again, a very useful sequence of lessons! Lots of useful bits and pieces, and some good clarification about scope. Lots of good tips with finding code from Stack Overflow etc, Error Handling was clarified a bit, the use of tags and how to simplify when having multiple objects...plenty to be pleased about! Next time, I'm going to adapt the 'Dicee' app to make my own Yahtzee one. That will be a challenge but even if it doesn't all go to plan, it would be good practice!
Comments
Post a Comment