Odd results in code example

I’ve followed along with the code to the end of the chapter. I mainly works, but the change population function doesn’t work as I expected. If I refer to it as myTown.population, it does not show 10 less than before. But if I refer to it as self.population it works as expected. I put some extra print statements in to try to shed some light. It’s probably something silly, but I don’t see it.
Here is the Town struct and the debug output pasted in.
Thanks for your help.

struct Town {
var population = 5_422
var numberOfStoplights = 4
mutating func changePopulation(by amount: Int){
population += amount
print(“pop after func in changepop is (population)”)
}
func printDescription() {
print(“The town is (myTown)\n”)
print(“Population: (self.population), number of stoplights (myTown.numberOfStoplights)”)
print(“Population: (myTown.population), number of stoplights (myTown.numberOfStoplights)”)
}
}

Monster is terrorizing the town!
The town is Town(population: 5422, numberOfStoplights: 4)

Population: 5412, number of stoplights 4
Population: 5422, number of stoplights 4
true
Program ended with exit code: 0

struct Town {
    var population = 5_422
    var numberOfStoplights = 4
    
    mutating func changePopulation(by amount: Int) {
        population += amount
        print("pop after func in changepop is \(population)")
    }
    func printDescription() {
        print("\nThe town is \(self)\n")
        print("Population: \(self.population), number of stoplights \(self.numberOfStoplights)")
    }
}

var town = Town()

town.printDescription()

town.changePopulation(by: 10)

town.printDescription()

this just work fine to me

OUTPUT:

The town is Town(population: 5422, numberOfStoplights: 4)

Population: 5422, number of stoplights 4
pop after func in changepop is 5432

The town is Town(population: 5432, numberOfStoplights: 4)

Population: 5432, number of stoplights 4

Thanks for looking!
I see I could have made much clearer print msgs.
My confusion is that myTown.pop and self.pop aren’t the same, and the book says they should be.

The struct is a blue print for the actual instance of an object. when you create myTown, something along the lines of

var myTown = Town()

The only place you generally reference self is in the struct itself not when utilizing it from elsewhere. Adding a second town and manipulating that will show it better.

var mySecondTown = Town()
mySecondTown.changePopulation(by: 100)

Then print them both out. using their own .printDescription() functions.

myTown.printDescription()
mySecondTown.printDescription()

structure itself is a blueprint, not the “instantiated” struct that you use. That’s why self shows up in the struct definition, but wouldn’t in new functions that will affect a passed in town for example in a separate struct or class.

Oh, I see! Adding mySecondTown makes it clear.
That’s an idea that will stick now.
Thanks for your help.
John

Sorry, I’m still confused. Is the text in the book incorrect? I had the same issue and it was fixed when I made the changes that John suggested (changing myTown to self).

When printDescription was a function in main, you needed to use myTown.population and myTown.numberOfStoplights to access those attributes within the structure created by main. But when printDescription was moved to be a function in Town, it became able to access the structure’s attributes directly, just like changePopulation does. If you look at Listing 15.5 on page 172, you’ll see that the leading myTown. is removed from both attributes in printDescription, and self. is not added in its place.

1 Like

JonAult,

Thank you! This was the problem! And it was changed in the Town.swift and works perfectly!