Nick Walter Challenges Part 9 (Protocols)

Last one! After this I'm going back to Angela's course to look at Swift in action, alongside various other new things. The point of Nick's courses for me is to make me feel more confident and secure about the syntax of Swift, but to also apply the knowledge to these mini challenges. They have truly made me feel more assured about what I know with Swift. Definitely worth doing! So the last bit from Nick - Protocols!

Start Time - 19:24

Protocols

So a protocol is a series of rules that need to be adhered to.

protocol Age {
    var age: Int {get}

}

Inside the curly brace you can get or get set but not just set!

protocol Age {
    var age: Int {get set}
}

class Dog: Age {
    
    var age = 0
}

protocol Age {
    var age: Int {get set}
    
    func tellMeAboutAge() -> String 
}

OK I've learned something here. In the protocols you don't actually put any of the code in! You just put what needs to be returned, or what the outcome is I suppose. 

protocol Age {
    var age: Int {get set}
    
    func tellMeAboutAge() -> String
    
    init(age: Int)
}

class Dog: Age {
    
    var age = 0
    
    required init(age: Int) {
        self.age = age
    }
    
    func tellMeAboutAge() -> String {
        return "The age is \(age)"
    }
}

So an init which is from the protocol needs 'required' when used in a class. Again, in the protocol, it's just the main declaration without any actual body/code inside any curly brackets. 

OK not all of this is working - not sure why but am gonna skim over some as it could be due to older Swift version. 

Paused at 19:51; restart at 20:21

Delegation

protocol Toast {
    func makeToast()
}

class Toaster: Toast {
    func makeToast() {
        
    }
}

class Human {
    var name = ""
    var toastDelegate: Toast = Toaster()
    
    func makeBreakfast() {
        toastDelegate.makeToast()
    }
}

The point here is that using protocols makes your code more organised. Delegates are protocols essentially. 

Challenge

OK here we go!













So the first four look fine actually. The last one looks harder but I will make it work. Here we go!

struct OakridgeTeacher: Teacher {
    var experience = 0
    
    var name = ""
    
    init(name: String) {
        self.name = name
    }
    
    func instruction() -> String {
        return "Sit down and be quiet!"
    }
}

Challenges 1 to 4 done!

Now let's tackle 5....

func printTeacherExperience(name: Teacher) {
    let yearsExp = name.experience
    print("The teacher has been teaching for \(yearsExp) years")
}

OK so there we go protocols done and successfully so! Mostly made sense. Still need a bit of practice with this. Well, I feel good for doing so well with these challenges, even though they were at a 'beginner' level. Moving on to Angela's next!

Finish Time - 20:43 (Total time today: 49 minutes)

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)