Nick Walter Challenges (Part 7)

So having smashed all of the class-related challenges easily, I'm cracking on with Enumerations and whatever is next. Going to skim through some of the course content first. Let's go!

Start time - 17:54

Enumerations

Rick describes these as a 'make your own Bool'. That makes sense as there are a finite number of cases/options.

enum personalInfo {
    case name(String)
    case birthday(Int, Int, Int)
}


let newName = personalInfo.name("Josh")

This is a good example of declaring the type in brackets for the case values. 

enum Compass: String {
    
    case North = "North"
    case South = "South"
    case East = "East"
    case West = "West"
}

let direction = Compass.East.rawValue

So specifying the type gives raw values. 

enum DriverOrder: Int {
    
    case Hamilton = 1, Vettel, Raikkonen, Bottas, Verstappen, Ricciardo
}

let driver = DriverOrder.Bottas.rawValue

Xcode picks up that Vettel would be 2, Raikkonen 3 etc. 

Right all that seems fine and to be honest, I had forgotten some of it. So good to go over!

Enumerations Challenge





So 1 and 2 should be fine. 3 includes classes so quite a lot here! I will get it though. 

//1
enum Cutlery {
    case Fork, Knife, Spoon
}

So one was easy. Two should be OK too. 

//2
enum Cutlery: String {
    case Fork = "Fork", Knife = "Knife", Spoon = "Spoon"
}

let newSpoon = Cutlery.Spoon.rawValue

Did the last part to check it worked and it did!

class Dog {
    
   var name = ""
   var age = 0
   var breed = Dogbreed.Poodle
    
    init(breed: Dogbreed) {
        self.breed = breed
    }
}

var newDog = Dog(breed: Dogbreed.Other)

I've played around with this - didn't have to have an init but thought why not! Had to give breed an initial value. Let's check solution! Yep all good!

Paused at 18:24 (30 minutes so far)
Continued at 18:55

Error Handling

I found this so confusing before so hopefully this will help clarify things.  So it's when things go wrong and having custom built error messages. 

On newer Swift, just Error is used instead of ErrorType - 

enum DogError: Error {
    case EmptyString
    case NegativeAge
    
}

class Dog {
    
    var name = ""
    var age = 0
}

Right so fiddly syntax to include the throws with the errors etc. 

class Dog {
    
    var name = ""
    var age = 0
    
    func updateName(name: String) throws {
        
        if name.isEmpty  {
            throw DogError.EmptyString
        }
        self.name = name
    }
}

A couple of keywords - throws and throw used as above. 

dog.name = "Steve"
try dog.updateName(name: "Bob")

  func updateName(name: String) throws -> String {
        
        if name.isEmpty  {
            throw DogError.EmptyString
        }
        self.name = name
        return name
    }
}

Right so this is supposed to be better as you get to RETURN something. 

do {
    
try dog.updateName(name: "Bob")
    
} catch {
    DogError.EmptyString
}


try? dog.updateName(name: "")


Challenge










enum ErrorMessage: Error {
    case Error1, Error2, Error3
}


func errorHandler(name: String) throws -> String {
    
    if name.isEmpty {
        throw ErrorMessage.Error1
    }
    return name
}

do {
    try errorHandler(name: "")
} catch {
    ErrorMessage.Error1
}

try? errorHandler(name: "Steve")

That all seems fine - had to check my own examples with this one. Does not come naturally!

I could have done it within a class too. 

Yep all fine!

Stopped - Time 19:27 (32 minutes more; 1 hour and 2 minutes total)

And that's a good point to stop. I could keep going but too much in the evening makes my eyes hurt and harder to sleep! So lots of useful stuff here, definitely making more sense with Error Handling - had completely forgotten the do, catch, try etc. So Enumerations and Error Handling both making more sense in my head now. Good stuff!

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)