Object-Oriented Swift - Classes

Yep, two in a day - I couldn't resist! A great sign that I want to work through more coding! I'm picking back up with where I got to - from structures to classes...

Introduction to Classes

The syntax for these is similar in terms of the UpperCamelCase etc. A variable is used as a property rather than a constant because this value is likely to change.

One of the differences with structs and classes are that we almost must write the init methods in the latter. Initializer methods are always just init, no need for prepositional phrases etc.

class Enemy {
    
    var life: Int = 2
    let position: Point
    
    init(x: Int, y: Int) {
        self.position = Point(x: x, y: y)
        
    }
    
    func decreaseLife(by factor: Int) {
        
        life -= factor
    }

}

The whole init/self thing seems cumbersome but it's something to get used to for Classes!

Building a Tower

Pasan mentions that there other differences between classes and structs, which we will find out about later...


class Tower {
    
    let position: Point
    var range: Int = 1
    var strength: Int = 1
    
    init(x: Int, y: Int) {
        
        self.position = Point(x: x, y: y)
    }
}

Challenge: I needed to create a class and do various things with the initialiser method. Not easy but I cracked it, again without help!


struct Location {
        let latitude: Double
        let longitude: Double
    }
    
    class Business {
        
        let name: String
        let location: Location
        
        init (name: String, latitude: Double, longitude: Double) {
            
            self.name = name
            self.location = Location(latitude: latitude, longitude: longitude)
            
        }
        
    }
    
    let someBusiness = Business(name: "Business", latitude: 4.5, longitude: 2.8)

Helper Methods

A point Pasan makes when creating a method for firing at the enemy, not to create a value for whether the tower is in range within the function. It would make it too local!

This is very complex - using loops, if statements...bit lost at the moment! Looking through it again, I'm not too concerned - it is logical but I need more experience and practice with combining so many elements of Swift together. 

Class Tower {
    
    let position: Point
    var range: Int = 1
    var strength: Int = 1
    
    init(x: Int, y: Int) {
        
        self.position = Point(x: x, y: y)
    }
    
    func fire(at enemy: Enemy) {
        
    }
    
    func isInRange(of enemy: Enemy) -> Bool {
        let availablePositions = position.points(inRange: range)
        
        for point in availablePositions {
            if point.x == enemy.position.x && point.y == enemy.position.y {
                return true
            }
        }
        return false
    }
}

This 'helper' method - I think it's called this (the in Range one) is called so as it helps the functionality of another method. 

High Level Overview

Pasan does a recap of the last few videos. One of the key points is an instance has to be created for any of the code to actually be used. 

Well, that was the trickiest video so far. There was a lot combined from previous videos, but the conceptual understanding of it all was not straightforward. Basically, it was hard to follow! The good thing though is that I'm gaining in experience all the time. The fact that it was hard to follow shows that I'm being stretched. Plus it all wasn't just a blur - I could make sense of what he was doing and why, it was just confusing having so many functions, with various properties from previous ones being used! Next time, there will be work on inheritance and overriding - concepts I'm familiar with. I'm still buoyed by the fact that I WANT to do this and learn Swift. I'm not going to use the fact that the code has become more challenging as a reason to lose any interest whatsoever. 

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)