Nick Walter Swift 5 Course - Part 7 (lectures 38 to 39)
So the final part of Nick's course! This last chapter, the iPhone app project has been more recap but still well worth doing! Only got about 20/25 minutes now for today so just enough time.
Start Time - 17:51
If Statements
Away from the project...so will just skim through this.
Some focus on other operands e.g. < > == != <= etc.
Challenge....why not!
Make a var called temp to hold the temperature. Then create if statement to show if temp is greater than or equal to 30, print it's hot, if not, then print nice weather.
Not even going to use a playground for this!
var temp = 42
if temp >= 30 {
print("It's hot!")
} else {
print("Nice weather!")
}
There we go. Too easy.
Counting
Right, so Nick states that the idea is to have 10 clicks and then the background colour changes. I'm just going to do this myself - good practice!
DONE!
Awesome! OK, now looking at what Nick's done, he's clarified to show the counter.... I can do that!
Nice! And I've cleaned up the code to avoid repetition -
Start Time - 17:51
If Statements
Away from the project...so will just skim through this.
Some focus on other operands e.g. < > == != <= etc.
Challenge....why not!
Make a var called temp to hold the temperature. Then create if statement to show if temp is greater than or equal to 30, print it's hot, if not, then print nice weather.
Not even going to use a playground for this!
var temp = 42
if temp >= 30 {
print("It's hot!")
} else {
print("Nice weather!")
}
There we go. Too easy.
Counting
Right, so Nick states that the idea is to have 10 clicks and then the background colour changes. I'm just going to do this myself - good practice!
DONE!
var total = 0
var colourArray = [UIColor.green, UIColor.blue, UIColor.yellow, UIColor.purple, UIColor.orange, UIColor.brown, UIColor.red]
@IBOutlet weak var textLabel: UILabel!
@IBAction func changeText(_ sender: Any) {
textLabel.text = "The text has now changed!"
total += 1
changeBackgroundColour()
}
func changeBackgroundColour() {
if total % 10 == 0 {
view.backgroundColor = colourArray[Int.random(in: 0...6)]
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
changeBackgroundColour()
textLabel.text = "The Games Begin!"
}
}
Nice! And I've cleaned up the code to avoid repetition -
import UIKit
class ViewController: UIViewController {
var total = 0
var colourArray = [UIColor.green, UIColor.blue, UIColor.yellow, UIColor.purple, UIColor.orange, UIColor.brown, UIColor.red]
@IBOutlet weak var textLabel: UILabel!
@IBAction func changeText(_ sender: Any) {
total += 1
updateInfo()
}
func updateInfo() {
if total % 10 == 0 {
view.backgroundColor = colourArray[Int.random(in: 0...6)]
}
textLabel.text = String(total)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
updateInfo()
}
}
And that's it! I've posted in the comments - thought it would be good to do this as it means I'm really understanding the code better!
Finish Time - 18:15 (24 minutes total - not much but productive!)
So course complete! Going to look at Stephen Destefano's next. I went for this as a) it was reduced t0 $10 and also it has lots of built in quizzes along the way - a good way to test myself!
All done for now!
Comments
Post a Comment