Instances of myTown created for each monster?

In this chapter, we created a Town structure and a myTown instance of that structure. After going through the chapter and the challenges, if I add a statement at the very end of myTown.printDescription() there is no change to the description from the very beginning, even after all the zombie/vampire terrorizing and thrall making!

Is this because each time a monster is given a town to terrorize, it is actually creating a copy of the original instance of myTown?

If that is the case, if we made Town a class rather than a structure, would it solve this problem? So that the zombies and thralls would be terrorizing the same town?

I’m trying to wrap my head around why these statements don’t make a difference to mytown.printDescription():
fredTheZombie.terrorizeTown()
fredTheZombie.town?.printDescription()
vicTheVampire.terrorizeTown()
vicTheVampire.town?.printDescription()
print(“There are now (vicTheVampire.thralls.count) thralls!”)

I posted too soon before I just tried changing the definition of Town to a class, and removing “mutating” to the changePopulation function. Sure enough, it works. Once it’s a class, then every time you assign myTown to a monster, you’re only assigning a reference to myTown rather than creating a copy of a structure, and the population counts drop as expected.

1 Like

In the next chapter they talk about ways to solve that problem without changing Town to a class. Which is not to say that changing Town to a class is a bad idea, just that there are other things to consider. Chapter 18 is pretty much all about the tradeoffs between classes & structs and when to use each.