Ray Wenderlich Functions and Types Course - Part 12

Here we are! This will be to conclude Catie's course. It's been useful to go over the content. The issue is that none of it is project-based - I need to see more in context! The MOST useful way of using the info will be in the consolidation entry to follow this. So here we are, the rest on protocols and inheritance!

Start Time - 12:56

Protocols and Extensions

Extensions - I know about these already. Extending them to protocols...

Using protocols you can say 'composition'. If class inheritance isn't quite working then protocols are the way to go!

Extensions can't have stored properties or designated inits.

At the moment I don't see the advantage of having extensions for a protocol when all of that could just be in one protocol.

You can also extend native types e.g. Ints.

This is quite advanced!

extension Numeric {
    var squared: Self { self * self }
}


An example of putting in member-wise init. 

Challenge! Last one for the course...

First part all seemed OK....

protocol Shape {
    var area: Double { get }
    
}

struct Square: Shape {
    var sideLength: Double
    
    var area: Double {
        get {
            sideLength * sideLength
        }
        set {
            sideLength = newValue.squareRoot()
        }
    }
}

struct Triangle: Shape {
    var base: Double
    var height: Double
    
    var area: Double {
        get {
            0.5 * (base * height)
        }
    }
}

struct Circle: Shape {
    var radius: Double
    
    var area: Double {
        get {
            (3.14 * radius) * 2
        }
    }
}


One mathematical error - circle should be pi x radius squared. So I've used the .pi property and multiplied that by radius times itself.

Also, I put in the setter for square's area, so that the area can be entered, then the sideLength calculated from that!

I missed the second bit of this. I needed a little help with the map bit. Just a regular map needed rather than anything too fancy!

var square = Square(sideLength: 5)
square.area
square.area = 100
square.sideLength

var triangle = Triangle(base: 4, height: 3)

var circle = Circle(radius: 5)

var shapeArray: [Shape] = [square, triangle, circle]

let shapeAreas = shapeArray.map { $0.area }

print(shapeAreas)

Wow second part was tough and out of my area of understanding! But good to see.

// -----------------------------------
func isInteger(number: Double) -> Bool {
  number.rounded() == number
}
// -----------------------------------

// TODO: Write solution here


extension FloatingPoint {
    
    var isInteger: Bool {
            rounded() == self
    }
}


let double: Double = 5.0
let float: Float = 5.4

double.isInteger

float.isInteger



Paused at 13:38; restart at 15:32

Last bit - value vs reference types

So SwiftUI is more struct based and UIKit more class....interesting!

OK for struct, if they are equal then considered the same - dumbbell analogy. 

Class - no two are the same! Even if values are the same (shoe analogy). 

Object is a CLASS instance. Value is a STRUCT instance. That's interesting!

If you can't choose, start with a struct then make it a class if need be. 

When to subclass?

Single responsibility - best practice for each class to have a SINGLE CONCERN

Type safety - good for class

Shared base class - link to polymorphism

Extensibility - if extending code you don't actually own!

Identity - if it's about what the objects are, use class. If it's for behaviour/what can be done then use protocol!


Finish Time - 15:40 (50 minutes)

There we go! Course complete! Catie has proven to be a great instructor - one of the better ones for sure. Will definitely check out her other courses, if/when they happen. A bit at the end was where to go next...a few options there! This particular chapter was a great way of refreshing the use of protocols within classes and structs. The concepts of init and subclassing have become clearer to me. Before I start the next course, I will do a consolidation entry presently...


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)