Posts

Showing posts with the label Initialization

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

Image
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 th...

Nick Walter Challenges (Part 6)

Image
Next part - all linked to classes! We have inheritance and initialization - two areas I do need a clearer understanding. Let's go! Start time - 18:09 Inheritance - Subclassing class Animal {     var name = ""     var age = ""          func sayHello() {         print ( "Hello! I am an animal!" )     } } class Mammal: Animal {     var hairColour = ""      } let snake = Animal () let dog = Mammal () dog . hairColour = "brown" dog . hairColour class Monkey: Mammal {     var thumbSize = 0 } let george = Monkey () george . thumbSize george . sayHello () Nothing new here to be honest but nice to recap! Overriding Use of the override keyword if you use the same name of the function from the super class but change its functionality in some way. Easy.  Use of the super. option - thi...

Treehouse Intermediate Course Part 4 (Initialization)

Image
I deliberately had a break yesterday - I figure it's health to 'let the information settle' and also not set an unrealistic precedent of coding every day! If we're aiming for the three hours per week, then that can start from NOW and we'll assume I hit that target last week! So continuing with initialization. Let's go! Designated and Convenience Initializers So Pasan makes the point that in value types (e.g. structs/enums) initialzation is only needed when doing a custom one. For reference, it is necessary and is important to refer to the super/base class... Classes have two types of initializers. Designated - central point of init, classes must have AT LEAST one of these, they're responsible for initializing stored properties, and responsible for calling the super init. So so far we have been using designated ones. Classes typically have one - they have to have at least one. We'd get an error without using an init method. class Vehicle { ...

*EXTRA ENTRY* Properties and Initializers - recap!

Technically, I promised not so many entries but I really feel the need to go over some of this! There was a lot of content in the last couple of sessions that I did not feel were clearly explained enough - no offence Pasan but they were hard to follow! So now is the time to seek out a bit more information. This is very 'casual' - I have an Avengers film on in the background and am not going under any time constraints or anything - I have plenty this evening. So I just want to clarify a few things and get some hands-on practice. Let's go! Properties From Pasan's explanations, there are several different types of these. This is what I know: Stored properties - these are values which are either declared within the class/struct/enum, or declared upon initialization.  Computed properties - these require a calculation and a reference to one of the stored properties, which may not yet have a value. The computed property can be accessed when an instance is create...