Learn Courses My Dashboard

Module 2: Challenge 6

I followed the lesson 6 video and I came to the same conclusion in the challenge.

class Entity{
    private var name = ""
    func setName(myname:String){
        name = myname
    }
    func EntityType(){
        print("I am a \(name) Entity")
    }
}

class Tile: Entity{
    private var solid = true
    func setCollision(isSolid:Bool){
        solid = isSolid
    }
    override func EntityType() {
        super.EntityType()
        print("My collision type is: \(solid)")
    }
}

class Mob: Entity{
    private var walk = false
    func setWalking(isWalking:Bool){
        walk = isWalking
    }
    override func EntityType() {
        super.EntityType()
        if(walk){
            print("Mob is currently walking")
        }
        else{
            print("Mob is not walking")
        }
    }
}

//Both are referencing the same Mob class
var albert = Mob()
albert.setName(myname: "Soldier")
albert.setWalking(isWalking: false)
var bob = albert
bob.setName(myname: "Engineer")
bob.setWalking(isWalking: true)

//Displays the entity names
print(albert.EntityType())
print(bob.EntityType())

//Since we are passing the variable by reference both Bob and Albert reference the same MobType memory location meaning that if we change bob to something else than albert changes to the exact same thing.

struct Enemies{
    private var name = ""
    private var walk = false
    
    mutating func setWalking(mywalk:Bool){
        walk = mywalk
    }
    
    mutating func setName(myName:String){
        name = myName
    }
    
    func EnemyType(){
        print("Enemy is: \(name)")
        if(walk){
            print("Is currently walking")
        }
        else{
            print("Is not walking")
        }
    }
}

//Both are referencing the same Enemy structure
var firecat = Enemies()
firecat.setName(myName: "Flamecat")
firecat.setWalking(mywalk: true)

var zaphound = firecat
zaphound.setName(myName: "Lightningdog")
zaphound.setWalking(mywalk: false)

//Displays the enemy names
print(firecat.EnemyType())
print(zaphound.EnemyType())

//When the variable zaphound is set to firecat, it actually makes a duplicate copy of the enemy structure that firecat uses even though we only have one instance of the enemy structure. When that copy is made we now have two unique variables rather than one using a class.

//When variables are created in a structure each is assigned to be immutable by default. Each method follows this format immutable func setName() for structures. To make these mutable we pass in the mutating keyword to each function. This allows variables name and walking to be changed from the data being passed in from the let variable.

//When variables are created in a class, each are mutable by default meaning that each method for a class is written as mutating func setName(). Since this is the default behavior for classes the mutating keyword can be safely removed.

//Since we are passing the variable by reference both Bob and Albert reference the same MobType memory location meaning that if we change bob to something else than albert changes to the exact same thing.

//When variables don't need to change in functions you can easily leave off the mutating keyword in the structure. For those that do need it, you will need to type in the mutating keyword.
1 Like