Ray Wenderlich Fundamentals Course - Part 2

Yes! First consecutive entry for a little while. Good news - I have sorted out the UI stuff so I have Mac OS Catalina - the beta version. I've changed my account so I get to have the beta stuff basically - ahead of time. So when the actual update comes up, I will just get that for the 'normal Xcode'. Anyway, I want to complete the fundamentals course. It's been very, very basic so far. So will skim through the content and just get to the challenges. Also, I'm aiming for no more than an hour at a time, but in 2 possible stints today. So stint 1 is to get the next chapter done. Here we go!

Start Time - 11:49

COLLECTIONS

Tuples

One thing to type - that you can assign multiple values in brackets to the entire tuple, then each of those values is saved separately.

let wrestlingFacts = (name: "Kevin Owens", didWin: true)

let (name, wonMatch) = wrestlingFacts

So now 'name' and 'wonMatch' are their own values. I could use the _ in this too for multiple values. 

Challenge - all done in 3 minutes!

Arrays

A list of values. Or a list of ELEMENTS. Only the same type. 

Operating on Arrays

Subscripting - goes from 0 etc. 

var wrestlers90s = ["Steve Austin", "Bret Hart", "Shawn Michaels"]
var wrestlers2000s = ["Brock Lesnar", "Randy Orton", "John Cena"]

var topTwo90s = wrestlers90s[0...1]

That's a good one. In Chris's example, he makes the type explicit with Array( ) around the line. 

Other operations - count, isEmpty, removeAll etc. 

Optional binding to check for a specific value. Technically .first would be an optional as it MIGHT not contain a value!

.contains as another - bool for true or false. Also remove (for a specific index), insert etc. For remove and index, there are automatic parameters that come up. Swap at as well - that's pretty cool!

Challenge - done in two minutes!

All spot on. Again, I didn't do the explicit type syntax for Array ( ) for the new array. But it still works. 

Chapter done!

Paused at 12:24 (35 minutes so far)

Restart at 12:51

CONTROL FLOW

While Loops

So the whole point of loops is that they follow the DRY principle. 


var x = 0

while x < 10 {
    print(x)
    x += 1
}

var i = 0

repeat {
    print(i)
    i += 1
} while i < 10

I've never really used while or repeat while loops in actual projects. 

Challenge - Done in 5  minutes. Last one was fun!

*Paused at 13:03, resumed at 13:34

OK, so slight misunderstanding - I thought it meant first 6 rolls, not the first 6 on the die being rolled! SO the last bit needed to be != 6. 

For loop

Just skimming through this. Onto challenge soon!

for i in 0...100 where i % 2 == 0 {
    print(i)
}

Nice use of the where clause - not so au fait with that one. 

Smashed the challenge (approx 5 minutes)

Iterating collections

So this is pretty cool!

let daysOfTheWeek: [String] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

let poolTemperature: [Int] = [78, 81, 74, 80, 79, 83, 84]

for i in 0..<daysOfTheWeek.count where poolTemperature[i] <= 81 {
    print("\(daysOfTheWeek[i]): \(poolTemperature[i])")

}

Now onto the challenges...

Done! Took a few minutes. At first I made it more complicated, then realised what we were getting at! The where clause definitely is a more elegant use of code compared to an if statement. 

Nested Loops

So these are loops within loops. 

for i in 0..<daysOfTheWeek.count {
    if daysOfTheWeek[i] == "Thursday" {
        break
    }
    print("\(daysOfTheWeek[i]): \(poolTemperature[i])")

}

So the break keyword means that NOTHING is executed after that point. 


for i in 0..<daysOfTheWeek.count {
    if daysOfTheWeek[i] == "Friday" {
        print("I'm in love")
        continue
    }
    print("\(daysOfTheWeek[i]): \(poolTemperature[i])")

}

In this one, there is a print line ("I'm in love") then the continue keyword means that the iteration continues!

OK in the example below, continue means that condition is ignored but everything after is:

for floor in 1...11 {
    if floor == 13 {
        continue
    }
    for room in 1...4 {
        if room == 1 {
            continue
        }
        print("Floor: \(floor), room: \(room)")
    }
}

So more below. There is a label for the first loop, which is referred to later on. Complex stuff and by far the hardest the course has been up to now. 

floor_loop: for floor in 1...14 {
 if floor == 13 {
        continue
    }
    for room in 1...4 {
        if room == 1 {
            continue
        }
        
        if floor == 12 && room == 3 {
            continue floor_loop
        }
        
        print("Floor: \(floor), room: \(room) is available")
    }
}

Challenge - phew! Tricky but did them all. The first bit was complicated by different syntax for the use of 'terminator'. Anyway, all done, the chapter complete and I'm done for today!

Finish Time - 14:56 (Total time today approx 2 hours - had a couple of other mini breaks I forgot to put in)

So a lot of good stuff here. I've picked up some useful bits - how to work nested if statements, the use of the where clause and more practice with while/repeat loops. Also some good stuff with saving tuples to another value and the same with arrays. Just two more chapters and the course is done!


Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)