Nick Walter Challenges (Part 5)

Yes I managed to find time for one more session! So here we go - now going on to properties. The next several will be all linked to class aspects, so I'm actually going to go through them before the challenges. Always worth building my knowledge up on these as I need to!

Start Time - 20:34

Computed Properties

So from what I can remember about these, the property is not stored but requires some sort of computation. In the example below, I have two stored properties:

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

}

So this is a computed property - it requires the use of the stored property of age and creates a temporary one of current year. Makes total sense. 

var birthYear: Int {
        let currentYear = 2018
        return currentYear - age
    }

This looks like a function - using return and all. I could actually use a function but a computed property is better! 

So an interesting question is raised - when do we use methods and when computed properties? Not using parentheses implies the latter. So .count is actually a computed property then? 

I think get and set make sense - property observers. Set means that you can manually input the value of the property rather than have some calculation/computation go on!

 var birthYear: Int {
        get {
        let currentYear = 2018
        return currentYear - age
        }
        set(newBirthYear) {
            let currentYear = 2018
            age = currentYear - newBirthYear
        }
    }

Right so get/set are not property observers - just remembered these are didSet/willSet! Anyway, the point of using get and set means that I can input the newBirthYear anyway, without the computational part. 

Still a bit confusing but I think I'm getting there. 

Lazy Stored Properties

So the code never gets run until the property is actually needed. Saving space and memory...

Observers

So THESE are the willSet/didSet! 

class Cat {
    var weight = 0 {
        willSet(newWeight) {
            print("The new weight is: \(newWeight)")
        }
        didSet {
            print("The weight has been updated")
        }
    }
}

var myCat = Cat()

myCat.weight = 8


Right so willSet/didSet are used above to show the system alerting you to a value being changed. 

Right NOW I am going to the challenge!

Properties Challenges





Right so for the first one I only need to have a get - I don't need a set as I won't be manually inputting this value. The lazy property - well I know the keyword. As for the property observer, I am using print statements...

Let's give it a try!

//1 and //2:

class Pizza {
    lazy var radius = 1
    var size: Double {
        get {
            let area = 3.14 * Double(radius) * Double(radius)
            return area
        }
    }
}


For the third one, I've got rid of the lazy keyword as property observers don't work on these. It all works!

class Pizza {
    var radius = 2 {
        willSet {
            print("The new radius is \(newValue)")
        }
        didSet {
            print("The radius has been updated")
        }
    }
    var size: Double {
        get {
            let area = 3.14 * Double(radius) * Double(radius)
            return area
        }
    }
}

var pepperoni = Pizza()

pepperoni.size

pepperoni.radius = 4

OK I've noticed I misread the instruction - have updated the willSet bit! I remembered the use of newValue and oldValue - useful!

class Pizza {
    var radius = 2 {
        willSet {
            if newValue > radius {
                print("The pizza is getting bigger!")
            } else {
                print("The pizza is getting smaller!")
            }
            
        }
        didSet {
            print("The pizza radius has been updated")
        }
    }
    var size: Double {
        get {
            let area = 3.14 * Double(radius) * Double(radius)
            return area
        }
    }
}

Let's just check the solution....

Perfecto! Only thing I could have added in as an extra was the use of oldValue along with newValue...

 didSet {
            print("The old value was \(oldValue) and is now \(radius)")
        }

That is useful to know!

Finish Time - 21:25 (51 minutes total)

Well that was all definitely worth it. I've strengthened my understanding of properties and applied it all really well to the challenges! Tomorrow I will have a look at inheritance then initialization. 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)