Sandra L Course Part 3 (lectures 16 to 22)

Well it's been a good few days! After so much success with my first proper app, it's been a shame not to capitalise on that. Having said that, I do feel recharged and ready to learn some more! My plan with Sandra's course is to keep with it for all of the recap. When it starts getting trickier, I'm going to work on an Xcode project for another app idea - got a few possibilities! So, let's continue with Sandra's course!

Start Time - 13:29

Parameter Labels

func add(number1: Int, number2: Int) -> Int {
    
    return number1 + number2
}

add(number1: 4, number2: 6)


Again, easy stuff. The next step here is to use _ to have empty parameter labels. 

OK - one useful distinction here between parameters and arguments. The argument is the external name - the one used outside of the code block. The argument label should read as a sentence e.g. using 'to' instead of 'name' in a sayHello function. 


So "say hello to Thomas" is a sentence. This is more functional and user friendly. 

Challenge time!!

Challenge 3 - Rolling the Dice


OK, a challenge with some logic required. Let's break it down. First I need to have a random number generator, that links to the count in the diceFaces array. Then the function needs to return that dice face, based on the random number. OK, no problem!

Right. Not as simple! The previous way of doing random numbers with arc4random was always a bit complex with the UI32 type. So I found another way:

let diceFaces = ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"]

func rollDice() -> String {
    
    let randomDice = diceFaces.randomElement()
    
    return randomDice!
    
}


let diceValue1 = rollDice()

var diceResults: [String] = [diceValue1]

let diceValue2 = rollDice()

diceResults.append(diceValue2)

The uncertain bit here is the fact that the randomDice value needed unwrapping. 

OK, I can see why they have updated Swift. The UInt32 with all the typecasting...it does look confusing! 

Another way I've just done it:

func rollDice2() -> String {
    
    let randomNumber = Int.random(in: 0...diceFaces.count)
    
    return diceFaces[randomNumber]
    
}


This I prefer as there is no unwrapping!

Cool, good stuff! I also created an array to add results to, just to track these. But no need anyway. 

Challenge 4


Right, so straightforward stuff again. Just going to go straight in with each!


func add(number1: Int, number2: Int) -> Int {
    
    return number1 + number2
}


func subtract(number1: Int, number2: Int) -> Int {
    
    return number1 - number2
}

func multiply(number1: Int, number2: Int) -> Int {
    
    return number1 * number2
}

func divide(number1: Int, number2: Int) -> Int {
    
    return number1 / number2
}

Yes all very easy. Sandra does some other functions - for the sum of several values, averages etc. but they're quite long winded and not very logical. I know I can do these anyway. 

Right. End of chapter! Going to move on to the next....


For Loops

for i in 0...10 {
    print(i)
}

Key point is that i is a LOCAL variable. I cannot access it outside of the loop. 

All of the bits about half-open/closed - all easy. 

Functions within loops:


All that is fine. Next!

While Loops

So the basic syntax is different to For. Specifically, you need a while block of code with the condition before the curly braces. 

var index = 4

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

In that example, I've added one before printing, so it will be from 5 to 10. If I swapped over those lines, it would be 4 to 10. Simple!

Do-while*
Actually repeat while now!

My take - this is when you execute the do block, then have the condition at the end. Before Sandra shows this, let me try. Also, the point of this is that the code will go up to the condition but be one less. I think!

Oh most importantly, it is repeat-while instead of do-while now!!

var x = 7

repeat {
    print(x)
    x += 1
    
while  x < 12

Finish Time - 14:31 (1 hour 2 minutes total)

OK, some really good recapping here and a result finding out about the new way of randomising numbers! All of the function and loop stuff was logical. None of it was a great conceptual leap but I did find Sandra's way of doing things quite long-winded and unclear. Going to finish off chapter 4 next time. Then we're on to OOP, then an app! So sticking with it for now. 

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)