The momentum continues! Again, no need to get too excited - this is all recap, and easy recap at that. Still, it is necessary to do so. With Coursera, I will see what the rest of the content is like before deciding to sign up for another month or not. I still have a few months with RayWenderlich.com too.
Dictionaries
Another collection type - unordered unlike arrays with the key-value pair business.
Subscript - square brackets. Adding elements, clearing it - all the usual functionality. Using a for loop and tuple together - a good way to look through a dict for info. Using .keys and .values after the dictionary allows you to access each of those. Apparently when using JSON, you will be grabbing data from keys and values. Examples of getting info from JSON from Star Wars and Pokemon.
Challenge - one question as usual. I misread this before as 5 questions when it was a '5 minute' challenge. Of course it's not 5 minutes but that makes more sense now! Now onto the playground challenge that never gets checked. Done!
Functions
22 minutes - oh wow I'm not sure if I can face that much of Mark, sorry Mark!
Code smell - something is up here! Spidey sense etc.
DRY - Don't Repeat Yourself!
A good point here is that for functions, you should use a verb. So 'area' is not as clear as 'calculateArea'.
An example to put in as it's one I made while listening to Mark, but my own idea. For taking a risk at a corner in a F1 game:
func payThePenalty(drivePoints: inout Int, requiredPoints: Int) -> Bool {
let difference = drivePoints - requiredPoints
if drivePoints >= requiredPoints {
drivePoints -= difference
print("You have \(drivePoints) points remaining")
return true
} else {
return false
}
}
var currentDrivePoints = 7
var requiredPointsCorner1 = 4
payThePenalty(drivePoints: ¤tDrivePoints, requiredPoints: requiredPointsCorner1)
currentDrivePoints
An example that contains a few elements at once - parameters, inout (when one parameter can mutate), return type and print within, if statement. Good stuff!
Challenge - question fine but double checked in playground as wasn't sure about the example with Void. Playground challenge - nothing there. Disappointing!
Optionals
Something I've always found tricky! So need to pay extra attention here...
I like Mark's example with the database name with middle names. Makes good sense!
OK my own example with another (geeky) interest of mine - Buffy!
struct BuffyCharacters {
var firstName: String
var middleName: String?
var lastName: String?
func printFullName() {
let middle = middleName ?? ""
let last = lastName ?? ""
print("\(firstName) \(middle) \(last)")
}
}
var buffy = BuffyCharacters(firstName: "Buffy", middleName: "Anne", lastName: "Summers")
buffy.printFullName()
var spike = BuffyCharacters(firstName: "Spike", middleName: nil, lastName: nil)
spike.printFullName()
This is cool because in the second example, Spike has no middle or last name. Therefore just "Spike" is printed when the printFullName method is called. Cool!
Optional chaining - use of if let to then test if a value is there, or is nil.
Forced unwrapping - will crash when the user is running (the worst time!). So don't force unwrap unless told otherwise. Can be useful.
Challenge - OK unusual one as there is an article to read. Let's do it!
https://hackernoon.com/swift-optionals-explained-simply-e109a4297298
Good article! Nice refresher - all the types of unwrapping, binding, chaining, nil coalescing.
FizzBuzz challenge - easy! Key thing was to start with the numbers dividing by 5 and 3 to get FizzBuzz.
Oh bigger assessment now - 20 questions! Ok first time 14 out of 20 where at least 3 were silly mistakes. Second time, 19 out of 20 where whatever you put, the last answer would've been wrong. So all good really!
And that's the week done! All in a couple of hours (with some breaks etc.). Definitely good to revisit all of this.
Comments
Post a Comment