Treehouse Intermediate Course Part 1 (properties)

*I wrote this on Wednesday 22nd August but it did not save! So here it is, albeit one day later! 

Well, here we go! Having seen enough of Ray Wenderlich's course, I'm pressing ahead with the next Treehouse 'track' - the "Intermediate" course. The reason I've gone with this is that Treehouse, although not cheap ($25 a month), is a good way of tracking and monitoring progress. The Udemy courses have been positive in parts - especially Angela Yu's, which I gleaned a lot from. Anyway, I'm going to start this Intermediate course and this time - honestly - supplement my understanding with further examples/ideas from any of the Udemy courses or whatever I can find online. Basically, rather than racing through it and trying to pack in as much as possible, I'm going to take my time and try to understand Swift at a deeper level. 

So, my aim is to complete this track in a month, which is very plausible as it is 9 hours. I'll add another couple of hours on for challenges etc. So let's say 12 hours total, with any supplementary resources. That equates to 3 hours per week - very doable!

3 hours per week is technically 25 minutes a day, but that needs to be flexible e.g. 1 hour one day and none the next etc. As long as I hit three hours per week, then I'm on track. Not three in one day! I'd say a maximum of an hour on any given day. OK, that feels good to map it out. Now let's go ahead. We're back with Pasan - let's go!

Introduction

OK, so it is going to be a 'grab bag' of going over various concepts. I think this means that it won't be project-based, but will going over and furthering understanding of key ideas. Right, let's get stuck in!

Stored Properties

These allow us to store values as part of an instance. During init, we assign values to these properties, or if they are optionals, set them to nil.

These are not the ONLY way to associate values within a struct.

Type properties....

struct Point {
    
    let x: Int
    let y: Int

}

So in the example above, these would be called stored properties. Simple enough.

struct Map {
    
    static let origin = Point(x: 0, y: 0)

}

In this example, an instance is not needed to access the values. The static keyword means that the values are not going to change. 

Ok interesting - if we make it a var, then you CAN change the values. However, an instance is still not needed at any point. 

struct NewMap {
    
    let origin = Point(x: 0, y: 0)
}

NewMap.origin

Right, so doing some investigating tells me that the static keyword must be used to then allow the property to be accessed. Unless of course an instance is created!

Basically, to decide when to use a type property, is by deciding whether it is worth having an instance created or not. At the moment, that seems fairly vague. 

A question asked about type properties and what they are got this answer:

The definition of 'Static' means 'non moving'. Its not connected to any particular instance. The type property origin is associated with the Struct map - not the instances. Static just means it remains associated with the Type, not particular instances - it doesn't need instances to be accessed.

OK so that helps to clarify a little more! Thanks 'krystennant'!


Static Property and Method

No longer have to initialize an object to access properties and methods. Let us design a struct that contains a type property and a type method.

Design Struct

Access the properties and methods
Anything that has static in front will not be overridden.

OK none of the code came up here! But in the example, it shows the use of static before var and functions. It also compares 'computed properties' which link to the get/set. Will come back to that another time!

Challenge - Type properties

Declare a struct named LevelTracker with a single constant type property maxLevel. Assign an Int value to maxLevel.

Here's what I've got...

struct LevelTracker {
  
  static let maxLevel = 20
  
}

Whoo! Now I could have specified the type of Int but no need as I gave the value of 20. Now the next part is Computed Properties - going to do that and the challenge then call it a day for now. 

Computed Properties

struct Rectangle {
    
    let length: Int
    let width: Int
    
    var area: Int {
        return length * width
        
    }
}

let rectangle = Rectangle(length: 4, width: 6)
rectangle.area

So this is a COMPUTED PROPERTY - this doesn't store a value but computes it, based on other stored values in the class. 

Computed properties are also INSTANCE properties - they need an instance. 

Scope is importnat - variables at the same level etc. 

Area is a read-only computed property. 

Computed properties ALWAYS require TYPE ANNOTATION i.e. you must declare the type for these!

struct Size {
    var height = 0
    var width = 0
    
}

struct Rectangle2 {
    var origin = Point()
    var size = Size()
    
    var centre: Point {
        get {
            let centreX = origin.x + size.width/2
            let centreY = origin.y + size.height/2
            
            return Point (x: centreX, y: centreY)
        }
        
        set (centreValue) {
            
            origin.x = centreValue.x - size.width/2
            origin.y = centreValue.y - size.height/2
            
            
        }
    }
}

var rect = Rectangle2()

rect.centre

rect.size = Size(height: 4, width: 6)

rect.centre = Point(x: 2, y: 3)

Right so a LOT here! First of all, I've changed the Point struct so that initial values of 0 are set for x and y and are variables. 

The Size struct is to get a height and width (both at 0). 

Then the Rectangle2 struct uses the values of x and y from the point in the 'getter'. X and Y within Point are returned. Then the 'setter' creates a centreValue value, then does the same computation. 

What's the difference between get and set? Well, the get means that you will use the computed properties, whereas the set means you will CHANGE the values of the centre property upon initiliazation (the last row shown). 

Challenge - Computed Properties

In the editor I've created an enum to manage text presentation in my app. The enum members represent the three different text options.
Your task is to create a computed property, style, that returns the correct style specifier provided below. For example Text.Headline.style should return the string "UIFontTextStyleHeadline".

This is very hard!

let UIFontTextStyleHeadline = "UIFontTextStyleHeadline"
let UIFontTextStyleBody = "UIFontTextStyleBody"
let UIFontTextStyleFootnote = "UIFontTextStyleFootnote"

enum Text {
  case headline
  case body
  case footnote
  
  var style: String {
    
    switch self {
     
    case .headline: return UIFontTextStyleHeadline
    case .body: return UIFontTextStyleBody
    case .footnote: return UIFontTextStyleFootnote
}
  }    
}     

That was tough. I didn't think of the switch statement - which I should have thinking about it! And I had forgotten 'self' as what to switch, as there was no clear instance etc. 

Strange challenge to have, considering we had just done get and set!

So, just to makes sense of this:

Stored properties - these values are declared/accessed upon init in an instance
Computed properties - these require some sort of computation, using the stored properties. The getter and setter come in if you want the computed property to just be retrieving a value, or setting with new ones. 
Type properties - use the static keyword to show that there is NOT an instance used to access these. 

Ok, that makes sense!



Anyway, not a bad start! I've got a clearer understanding of what these properties are. Next time, I will continue with properties and hopefully clarify with more ideas in context!




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)