Coursera Introduction to Programming in Swift - Part 4

On a real roll now, yeah! I'm several weeks 'ahead of schedule', which is fine as this was very much a refresher for me. 

Object Oriented Programming

My understanding of this is the use of classes to then create objects, which you then essentially do the code with. Yes, a blueprint - having multiple copies so we're looking into inheritance. 

A good point he makes is you need to THINK in OOP. Those with design backgrounds take to this more easily than those who think with numbers etc. I'm a real mishmash on that one!

It's about thinking of the world in objects, then thinking of what properties they have and what they can do (methods/functions). 

Instances/Objects - that's a debate I remember from before. Isn't an instance of a value type e.g. a struct? Maybe not.

Instantiating - creating an instance from the class.

Pass by reference - where a constant object can be changed. You cannot copy an object. OK, in that example, you can put the OBJECT into the parameter of the function. I think I've got that - will take some getting used to. 

Inheritance

Long winded example with initialisation. After inheriting, you can then use the super.init, so anything from the original init is called before anything from the new init. The whole concept of inheritance is cool - the init part has always been fiddly and complex. 

Polymorphism

The condition of occurring in several forms. So, starting with a base class e.g. Shape that you can then adapt and inherit from, rather than creating a separate class for each shape. Yep got it - DRY principe etc. 

HOWEVER, wouldn't it be better to make a protocol to start with? No a protocol is better for specific behaviour than a certain object. 

Best to think of polymorphism of the use of inheritance - rather than creating a brand new class each time, starting with a base class and inheriting from there. Several forms implies that it happens for a few different ones, not just once! Poly technically means 'many'. 

Challenge - video link...

https://www.youtube.com/watch?v=pTB0EiLXUC8

Procedural programming - had lots of issues! Functions crossing over one another. 

OOP - the key thing is encapsulation. Putting together related properties and methods, then having as an object. Functions outside of classes have many parameters. Functions within classes are much less parameter dependent. 

Abstraction - I think the key point here is to have one function to do one clear thing. Not too sure about that. 

Benefits - grouping info together and reducing complexity

MVC in Theory

Another guy from Devslopes - Caleb. I know about this but not sure it is rather redundant with SwiftUI?

It is essentially a 'design pattern'. As he alludes to, it will be eventually replaced...has this happened already?

Model - a blueprint. How to make something. I think of model as the data e.g. information about songs. 

View - how things should appear on the screen. How they should look e.g. the face of a watch.

Controller - controls the view. Allows for interaction. Takes the data from the model and passes it to the view. 

Another article to read...

https://www.freecodecamp.org/news/model-view-controller-mvc-explained-through-ordering-drinks-at-the-bar-efcba6255053/

Cool analogy with ordering drinks at the bar - the model (ingredients and things to make drinks), the view (the actual drink) the controller (the bartender) and the user (the customer). 

Xcode Project

Cool! We're getting straight into a project. Exciting! This video must be before Swift UI, but still useful to do with the old school storyboard stuff. 

Setting up with folders for M, V and C. 

Playing at double speed as it is creating a model - a class of apple product. All straightforward. 

So the next bit is making a custom view, putting in properties by overriding the nibs function, then putting that into the options for the UIView on the storyboard. All fine so far. 

All of this works well - the putting in of labels, in a stack view, then creating an instance of apple product from the model...all working. Again, different to SwiftUI and definitely harder to get something basic up and running!

This next bit is about making the data safe. 

public private(set) var name: String

public private(set) var colour: String

public private(set) var price: Double


Confusing but basically, public means it is accessible, private with the (set) means you can set the value initially but it cannot then be changed. So accessing publicly but not able to change on a whim. 

Challenge -  create a new model for Samsung, new UILabel from nib properties, then assign that to the label on the view. 

All done! Used inheritance to make Samsung a subclass of Apple. 

Next challenge - didn't read this properly but I get the idea of this. The rest is just Maths. 

OK and now to the final assignment for OOP...

10 questions! 7 out of 10 the first time...not great but honestly, tricksy questions! Second time got 100%, so all good. 

And that's it! Refresher course done. Will check out what else Coursera has to offer next time. 







Comments

Popular posts from this blog

Coursera - Intro to IOS Development Part 1