Protocols in Swift - Part 3

I'm getting more used to protocols. This is also the first time in a little while I've managed two consecutive days! Hopefully it will become clearer with more experience. 

Protocol Inheritance

A point that Pasan makes is that you may not want to inherit like with classes as it takes on EVERYTHING. E.g. from a plane, you may only want to inherit the fly method for a bird.

protocol Printable {
    
    func description() -> String
    
}

struct User {
    
    let name: String
    let age: Int
    let address: String
    
    func description() -> String {
        
        return "\(name), \(age), \(address)"
    }
}

let user = User(name: "Josh", age: 33, address: "placeHome")

user.description()



But we need to include a more specific protocol, which inherits from the original one..


protocol Printable {
    
    func description() -> String
    
}

protocol PrettyPrintable: Printable {
    
    func prettyDescription() -> String
}

struct User {
    
    let name: String
    let age: Int
    let address: String
    
    func description() -> String {
        
        return "\(name), \(age), \(address)"
    }
    
    func prettyDescription() -> String  {
        
        return "name: \(name)\nage: \(age)\naddress: \(address)"
    }
}

let user = User(name: "Josh", age: 33, address: "placeHome")

user.description()

Another interesting point is that you can inherit from MULTIPLE protocols. This means you can adopt the behaviour/methods etc. from several at once! 

I nailed the challenge - creating a protocol, then struct through inheriting protocols. I then had to ensure that the properties in each protocol were in the struct. 

Swift's Standard Library Protocols

There are three main types... CAN DO - 

Used to represent behaviour when an object can do something. E.g. equatable. Looking at the link Pasan posted, they all seem to be 'able' ending e.g. printable, equatable etc. 

Static function is called on the type, instance method is called on the instance....

Then we have.... IS A

Modelling the identity of an object. So is this this to do with establishing what type an instant/object is?

Last we have....CAN BE

So e.g. any object that can be a certain type. ExpressibleByNilLiteral - Optionals use this. The naming across many protocols don't follow strict conventions. Using 'ExpressibleBy' is supposed to used instead of the convertible part...Pasan says not to worry about this for now so I'm not going to!

Something useful was clicking on the command button, then clicking on a keyword and it gives you comprehensive definitions - useful, didn't know that!

So that's all for now - just a bit today. Tomorrow I'm going to crack on and complete all of Protocols, then it means one more course (Error Handling) before I've completed the Beginner Track for Swift. Until tomorrow!

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)