Lynda Course - Swift 5 Essential Thinking (Part 2)

A bit of time for some Swift before bed! Made some good progress last time - covered a lot of the basics and consolidated well. Let's go!

Start Time - 20:37

3. Working With Collections

Right, playground file open and ready to go!

var levelDifficulty: [String] = []
var otherArray: Array<String> = []

I tend to use the first way (if empty array). 

Various use of .count, .isEmpty etc. 

An out of range exception error happens when trying something in the array that doesn't exist. 

Core Array Methods

Nice recap!

// Changing & appending items
var characterTypes = ["Dwarf", "Elf", "Goblin", "Ranger"]
characterTypes.append("Orc")

//Inserting and removing items
characterTypes.insert("Wizard", at: 5)
characterTypes.remove(at: 4)

// Ordering and querying values
var reversedCharacters = characterTypes.reversed()
characterTypes.sort()

characterTypes.contains("Elf")

// 2D arrays and subscripts

var skillTree: [[String]] = [
["Hook", "Drive", "Cut"], ["Googly", "Wrist spin", "Chinaman"], ["Catch", "Dive", "Throw"]

]

var cricketBowl = skillTree[1][1]

Haven't done much on the 2D arrays and subscripts bit. Cool - useful to know!

Dictionaries

// Creating dictionaries
var f1Wins: [String: Int] = ["Schumacher": 91, "Alonso": 32, "Hakkinen": 20]

// Accessing and modifying values
var schumiWins = f1Wins["Schumacher"]


// All keys and values

let allKeys = [String](f1Wins.keys)
let allValues = [Int](f1Wins.values)

Some good stuff here! The last bit with the all keys and values is new - cool! Key bit is to have the type in the square brackets, otherwise, you get a bit of gibberish rather than a nice, clear array!

(Delayed for 5 mins or so)

ar f1Stats: [String: Int] = ["Slipstreaming": 83, "Cornering": 75, "Engine power": 87]
var oldValue = f1Stats.updateValue(82, forKey: "Engine power")

f1Stats = ["Speed Demon": 79, "Latebraking": 98]

// Caching and removing items


// Nested dictionaries

var gameOfThronesFamilies = [
    "Lannister": ["Tyrion": "The Imp", "Tywin": "The don", "Jaime": "The Kingslayer" ],
    "Stark": ["Ned": "The daddy", "Rob": "The Young Wolf"]

]

var characterFinder = gameOfThronesFamilies["Lannister"]?["Jaime"]

Again, interesting stuff! I needed to put in a ? between the two keys in the nested one above. We would use optional binding normally. This is to ensure that there can be no nil value...still need to get my head around some of that!

Sets

// Creating sets
var f1DriverSet: Set = ["Alonso", "Vettel", "Leclerc"]

// Inserting and removing elements
f1DriverSet.insert("Hamilton")
f1DriverSet.remove("Leclerc")
f1DriverSet.contains("Alonso")


// More common methods
f1DriverSet.sorted()

I've never used sets much but useful to know!

// Test variables
var f1DriverSetFerrari: Set = ["Alonso", "Vettel", "Leclerc", "Mansell"]

var f1DriverSetMclaren: Set = ["Senna", "Alonso", "Hamilton", "Mansell"]
// Set operations
var commonTeams = f1DriverSetFerrari.intersection(f1DriverSetMclaren)
var allDrivers = f1DriverSetFerrari.union(f1DriverSetMclaren)
var differentDrivers = f1DriverSetMclaren.symmetricDifference(f1DriverSetFerrari)
var clippedDrivers = f1DriverSetMclaren.subtracting(f1DriverSetFerrari)

Tuples

A point I want to make before going into this, is that from the recent Pasan course, Tuples are essentially structs. They have the type declared and the main, multiple properties. Anonymous! That was the word I was looking for!

// Simple tuple

var driverInfo: (String, Int, Bool) = ("Alonso", 32, true)

driverInfo.1 = 33

var (name, wins, isChampion) = driverInfo



// Naming tuple values
var wrestlerFacts = (name: "Steve Austin", weight: 234, rumbleWinner: true)

wrestlerFacts.name


All good. As he mentions, it is better to use classes/structs for complex data structures. 

CHALLENGE!


// 1
var shopItemsArray: [String] = ["bananas", "cheese", "chocolate", "bluberries"]
var shopItemsDictionary = ["bananas": 0.87, "cheese": 2.55, "chocolate": 3.78, "bluberries": 2.50]

// 2
shopItemsArray.contains("crisps")


// 3
shopItemsArray.insert("grapes", at: 3)

// 4
var selectedItem = shopItemsArray[2]

// 5
var selectedItemPrice = shopItemsDictionary[selectedItem]

// 6
var fullArmorSet: Set = ["Sword", "Shield", "Breastplate", "Dagger"]
var currentArmorSet: Set = ["Sword", "Breastplate"]


// 7

var subtractedArmorSet = fullArmorSet.subtracting(currentArmorSet)

// 8

var armorPieceTuple = (name: "Sword", cost: 1300, canEquip: true)

armorPieceTuple.canEquip

No probs. I stumbled over 5 a bit - was overcomplicating it! All good in the end. 

Finish Time - 21:54 (Approx 1 hour 10 mins)

So collections done, and some useful stuff there! Will do a bit of a summary before the next chapter in my next entry, so that I can pick out the useful bits. 

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)