Stephen DeStefano Swift 5 Course - Part 5 (Lectures 17 to 22)

I'm back! So a good turnaround from the last entry! Yes, I'm going to make time! We're diving into classes and structures, which is always a useful comparison. 

Start Time - 15:42

Syntax



Ok so a good overview!

Syntax I'm fine with so just a recap here.

Properties - accessed with dot syntax.

Dot syntax - can be used several times e.g. if there is a custom type within the class/struct, then the property of that is used. Sounds complicated but actually very simple!

Initialisation -

 

A good definition! Stored properties cannot be in in an indeterminate state. That means they can't just be up in the air.... So either give default values within the body of the class/struct, or have them created upon the instance being declared.

So if a property will always take the same initial value, then it's best to do it as a stored property. Makes more sense then having to init this every time!

Value vs Reference Types

Ah the age-old debate!





In the example above, these are two completely different instances - behind the scenes. So if I changed the finalRating value for the like property, then the rottenTomatoScore like value would NOT change.



So if I checked the ticket price of theMartian instance, then it has also changed to 19! So this alsoTheMartian and theMartian - there is no copying going on. There is one reference.

Identity operators - === and !===

The === is IDENTICAL TO. This means that they would refer to the same class instance. Whereas == means the value is the same. So different things.

A good summary for when to use struct or class -



11 out of 11 in the quiz. Cool!

Paused at 16:01 (19 minutes so far)

Restart at 16:14

Initialisers

Going to skim through some of this and do some coding along on a playground. Just so I know that the syntax is right.

class Temperature {
    
    var temp: Double
    
    init(temp: Double) {
        self.temp = temp
    }

}


*On phone for approx 5 minutes

So the above is me just reminding myself. Stephen is rambling on somewhat on using inits with structs.

The struct init can get complex when using other custom types. So I'm not worrying too much about that at the moment.

Three more videos including this new one. That will do for today!

Two kinds of inits for classes - designated and convenience.

Designated is primary and will then be inherited and be the superclass. It is the main 'funnel'.

Convenience is secondary and more optional. It's supposed to be a shortcut and save time.

Designated always go up the chain of classes whereas convenience go left/right.

Override - you will need to use this in the subclass keyword if you want to change the init method.

class Personnel {
    
    var totalStaff = 20
    var description: String {
        return ("The total stuff number is \(totalStaff)")
    }

}

So in this, we have a stored property and a computed property. 

class Employee: Personnel {
    
    override init() {
        super.init()
        totalStaff = 25
    }
}

Now the property of totalStaff has changed for this subclass. The super.init() has to be used to access the super class's init. 

class Dog {
    var breed: String
    
    init(breed: String) {
        self.breed = breed
    }
    
    convenience init() {
        self.init(breed: "[Unnamed breed]")
    }
}

That's the syntax for the convenience init. The square bracket bit is confusing - could have just used a string there I think. Yes it's just within the string to make that text stand out. 

I get with convenience init that it's supposed to make life easier! So it should be quicker for when creating an instance. 

class BreedLifespan: Dog {
    
    var lifespan: Int
    
    init(breed: String, lifespan: Int) {
        self.lifespan = lifespan
        super.init(breed: breed)
    }
    
    override convenience init(breed: String) {
        self.init(breed: breed, lifespan: 12)
    }
}

So technical stuff when we combine inheritance and initialization. 

Required and failable init - 

So the required keyword is used to state that all subclasses need to use this init. They will also use the required keyword. 

And failable is built-in code not dissimilar to error handling. It means that nil will be returned if the init process fails. If the right info was not provided then it's a safe way of doing all that. 

E..g if an empty string were passed in and we did not want that to be possible. 

Finish Time - 17:09 (1 hour 4 minutes total)

So lots of good practice with classes - and structs to an extent! Good to go over syntax and focus on init for the last bit. Next time, Optionals!

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)