Posts

Showing posts from August, 2018

Treehouse Intermediate Course Part 7 (Extensions)

Getting some consecutive day entries in feels good. I'm pleased that I'm able to continue to learn a little bit here and there. The other day I felt a pang of frustration that I wasn't making leaps and bounds of progress and moving on to becoming an app developer, but unless I dedicated myself to it full time, this is NOT a target in the near future! It is a long term aim and there is no point rushing that. I'm getting the syntax and concepts into my long term memory this way. At some point I'll do a list of aims, but for now it is simply to complete the Treehouse Intermediate course, and at that point, pause my subscription and seek out some more practical challenges from Udemy. Last time it was about type methods and final classes - all that seemed ok. The first part for today is is about 'extending a native type' then on to protocols... Extending a Native Type Pasan threatens to 'blow my mind' - easy tiger! So adding minor functionality on top...

Treehouse Intermediate Course Part 6 (Type Methods and Final Classes)

Here we go! Just a little bit more today - which is best - little and often! Last time it was semantics for value and reference types. I'm starting with Type Methods.... Type Methods Before we used the keyword 'static' for properties, computed ones I believe. struct Point {     let x: Double     let y: Double } struct Map {     static let origin = Point (x: 0 , y: 0 )          static func distance(to point: Point ) -> Double {         let horizontalDistance = origin . x - point. x         let verticalDistance = origin . y - point. y                  func square( _ value: Double ) -> Double {             return value * value         }                  let horizontalDistanceSquare = square (horiz...

Treehouse Intermediate Course Part 5 (Semantics)

Here we go! Haven't had the chance over the past two days so am going to do a good half an hour or so now! Last time, it was a big focus on initialisers. I've made of sense of those and hopefully I'll be able to apply that. Now we're moving on to something else - can't remember. Anyway, let's go! Value Semantics Value types - structs, enums etc. struct Point {          var x: Double     var y: Double } var p1 = Point (x: 1 , y: 2 ) var p2 = p1 If we change the x value of p1, then let's see what happens to p2. It does not change!  A value type is COPIED ON ASSIGNMENT! We are copying the underlying value.  let p3 = Point (x: 2 , y: 4 ) Here, this constant is IMMUTABLE. You cannot then change the values! However with var (p1 or p2) you can change values, as long as the stored properties are variables (which they are!) struct AnotherPoint {          let x: Doubl...

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 { ...