Bob Lee Course Part 15 (Consolidation of Chapter 2 - lectures 16 to 25)

So, a great chance to make sense of all of the concepts in Chapter 2. I will then crack on with Chapter 3 tomorrow! Chapter 2 was all about Object Oriented Swift. A lot of the terminology was familiar but I do feel on the most part (other than singleton stuff...) that my understanding improved with all of this. So let's go over it now!

Start Time - 12:03

Convenience Init

My Take - this is very simply another way to initialize an object. It's called convenience because the block of code for that init method has it all going on, then the initialisation process is very simple, or convenient. It could be as simple as using () after the class name.

Yep, got it!

Here's my example from before:

class Human {
    var name: String
    init(name: String) {
        self.name = name
    }
    
    convenience init() {
        self.init(name: "Jeff Green")
    }
}
In this case, the convenience init has the name of Jeff Green built in, so an object with his name is created easily, conveniently. 

There was a colour example too - providing an extension on UIColor, so that a new colour could be created with much easier parameters/arguments to put in. 

On that note: difference between arguments and parameters...

Right the general consensus - though still not clear - is that arguments are used when calling the function, whereas parameters are used within the actual block of code for the function...

Computed Property

My Take - This is different to a 'stored' property in that some sort of computation is required in order to get the value. The type needs to specified and var is used. Get/set are optional. Get - means that a value is returned and may use another parameter value (SP). Set means that the value of the CP can be set manually instead and if so, any other SP involved in it needs to be set to the appropriate value. 

The example is not working - but that's because it's not within a class or method...

OK here we go:

struct Shape {

var radius: Double = 10

var diameter: Double {
    get {
        return radius * 2.0
    }
    set {
        radius = diameter / 2
    }
}

}

var newShape = Shape(radius: 12)

newShape.diameter

So either Bob did not let on that it wouldn't work outside of a class/struct, or that Swift has updated since then!

Property Observers

My Take - these are basically ways of warning us when there are going to be changes in the data. So didSet and willSet are used - didSet - the print line is read of what the value was changed to and willSet - means it is about to change. Used for tracking data basically. 

var name: String = "Josh" {
    
    willSet {
        print("Name is being changed")
    }
    didSet {
        print("Name was changed from \(oldValue) to \(name)")
    }
}

A little help needed to check over my code from before - oldValue is used rather than newValue! 

Failable Init

My take - this is similar to error handling in the sense that you specifically build in that something could fail - if the right value were not used for example. You use the ? after init and then run the code - using .isEmpty to explain what has to happen. 

Yep spot on. Example from before:

class Blog {
    
    let name: String
    init?(name: String) {
        if name.isEmpty {
            return nil
        }
            self.name = name
        }
    }

Of course, as it's an optional value, it would then need to be unwrapped. Essentially, the whole thing is safe as if there is no value, nil is returned - which is safe - or the value is, which is also safe. Either way, unwrapping would be needed as the ? after the init makes it an optional value.

Cool, moving on!

Override

My Take - So this keyword is essential after inheritance for any changes to init methods or functions in general. You have to use the 'super' keyword to access anything from the super class, to run the original block of code.

Example from before - in this case, the European subclass has a different init phase, as there has been another property added (city) that needs initialising. So we need to put that along with origin back into the init method, do the self line for the city property and the super line from the original line of code too. That is needed as it is a new init method.

class Human {
    
    var origin: String
    
    init(origin: String) {
        self.origin = origin
    }
}

let person = Human(origin: "China")

class European: Human {
    
    var city: String
    
    init(origin: String, city: String) {
        self.city = city
        super.init(origin: origin)
        
        
    }

}

Right the override word was not used at all as something was added rather than changed!

Two Phase Initialization

My Take - this is specifically for value types i.e structs and enums where you use several different inits so you have different options for initialisation. This is different to convenience as that is used only for classes, and is then supposed to make the creation of an object with that init easier and more convenient.

Yep that's the one!

struct NuclearRocket {
    var metres: Double
    var litres: Double
    
    init(metres: Double, litres: Double) {
        self.metres = metres
        self.litres = litres
    }
    
    init(feet: Double, gallons: Double) {
        let convertedMetres = feet * 3.28
        let convertedLitres = gallons / 3.75
        
    self.init(feet: convertedMetres, gallons: convertedLitres)
        
    }
}

The key point here is that you need to create temporary/local constants to store the conversion from feet to metres and gallons to litres.

Type Property and Method

My Take - this is the use of the 'static' keyword to make the property of function only accessed within the type - NOT in an instance.

Yes! An additional point for classes - there are other keywords to use here. Class - means that it is again only accessed within the type but this CAN be overridden (unlike static). Also 'final' - this cannot be overridden.

So that's pretty much it!

Singleton Pattern

My Take - WTF?! No, it's not that bad but I don't get this yet or see the purpose of it. Essentially, it means that the user has control over when code has been accessed. It can only happen once and with the private keyword before init, that print statement comes to the user and the user only.

OK, the other aspect is creating an instance of the type ONCE, WITHIN THE TYPE ITSELF! So that means, once that property is accessed, the object is created - the once only. Still don't really see the point!

Let me just see what else I can find out again...

Well, I have stumbled across something else - Bart Jacobs Cocoacasts. I've signed up to it for free so we will see if that will be of any use.

https://cocoacasts.com/category/swift

Anyway, it looks a bit beyond me at the moment, but I will check it out when the time is right!

Recap:

Chapter 2 has really helped me strengthen my understanding of a lot of core aspects of Object Oriented Programming. Here's what I have covered and feel more confident about:


  • Use of convenience init - another way (simpler) to initialise
  • Computed properties - use of get/set - how these can be used instead of functions
  • Property observers - tracking data and informing me 
  • Failable init - built in code used in case input is wrong so it is safer (optionals)
  • Override - used when original code is changed in some way - to init, to functions/methods
  • Two phase init - used in value types e.g. structs for two different options for init; different to convenience
  • Type property and method -use of static so the property is used within the type not as an instance/object. In classes, both 'class' - can be overridden and 'final' - can't be overridden.
  • Singleton pattern - used to have only one instance created but not a specific object made; private keyword used for main user to track when it has. 


Finish Time - 13:19 (1 hour 16 minutes total)

And that's chapter 2! Slow and steady progress is the key. My next plan is to complete Chapter 3 of Bob, then decide if I want to switch to Sandra's course or not. It's nothing personal, but it's healthy to have another instructor and have a fresh take on things. Looking ahead at chapter 3 - there are 6 meaty lessons there. Ones that I will do a consolidation of at the end of the chapter too. So at that point, I think I'll know whether I want to stick or twist!


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)