Lynda Course - Swift 5 Essential Thinking (Part 3)

So, predictably, it's been tough finding time this week. Knew it would happen with work becoming busier! So a quick-ish entry now. Next bit of Swift Essentials!

Start Time - 20:46

4. Application Control Flow

I love this! Probably my favourite aspect of Swift. Got enough time to complete this chapter as it's a short-ish one.


var hp = 91

var maxHP = 100

// If statement (<, >, <=, >=, !=, ==)

if hp < maxHP {
    print("Health is lower than the max health!")
} else if hp <= 50 {
    print("Health is running seriously low!")
} else {
    print("Health is not registered")

}

Optional Binding 

One interesting point is that the temporary value is only available within the if/let statement, not outside of that scope though!

Another example with multiple values. Optional chaining - 

if let _ = isShopOpen, let searchedItem = blacksmithShop["Shield"], let quest = questDirectory["Fetch Gemstones"]?["Objective"] {
    print("We have shop open with \(searchedItem) available for \(quest)")
} else {
    print("Not all information correct")
}


For In Loops


One-sided ranges are dangerous as they can be infinite! So the use of .... Open ranges I think they're called too. 

let playerGreeting = "Hello fellow Hunter!"
let armorTypes = ["Heavy Plate", "Hunters Gear", "Mage Robes"]
let weapons = ["Longsword": 150, "Dagger": 25, "Mace": 75]

// Strings and arrays
for character in playerGreeting {
    print(character)
}

for armor in armorTypes {
    print(armor)
}

// Dictionary key-value pairs
for (weapon, damage) in weapons {
    print("\(weapon): \(damage)")
}
// Using ranges
for indexNumber in 1...10 {
    print(indexNumber)
}

for armor in armorTypes[0..<armorTypes.count] {
    print(armor)
}

While Loops

I never tend to use these in real life. 

// While loop
var playerHealth = 5

while playerHealth > 0 {
    playerHealth -= 1
    print("Player health is \(playerHealth)")
}

playerHealth = 5

// Repeat-while loop
repeat {
    playerHealth -= 1
    print("Player health is \(playerHealth)")
}
while playerHealth > 0

Key difference - the increment/decrement happens first of all in repeat while. Then the print, then the condition. So it depends on what ou want it to do. 

Switch Statements

Yes, these are fun!

Not going to paste as not the best examples. 

One thing he's done is the use of if/let with optional binding in the code body of the statement/case etc. Could be useful!

// Test variables
let shopItems = ["Magic wand": 10, "Iron Helm": 5, "Excalibur": 1000]
let currentGold = 16

// Guard statement with for-in loop
for (item, price) in shopItems {
    guard currentGold >= price else {
        print("You can't afford")
        continue
    }
}


I need more practice with the syntax of guard until it becomes more automatic! The idea is to go negative first...


Mostly really good! Running out of time before bed for this last bit so rushed a bit. Needed help with the value binding bit within the switch statement. 

// 1
var lefthandWeapon: String?
var righthandWeapon: String?

// 2
if let left = lefthandWeapon, let right = righthandWeapon {
    print("Left weapon is \(left) and right weapon is \(right)")
} else {
    print("Not enough information")
}

// 3
var playerExp: [String: Int] = ["Steve": 24, "Lukas": 0, "Simon": 11]


// 4
for (attack, value) in playerExp {
    print("The player \(attack) has \(value) experience")
}

// 5
for (name, experience) in playerExp {
    guard experience > 0 else {
        print("\(name), you need some help!")
        continue
    }
    
    // 6
    switch experience {
    case 32: print("Good experience")
    case 200...500: print("Wow!")
    case (let localExp) where (localExp > 500): print("Whoah!")
    default: print("Unregistered experience level")
    }
}

That will do!

Finish Time - 21:44 (58 minutes)

All good stuff. More at the weekend!

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)