Listing 15-2 issue?


#1

When I type the code exactly as listed and then call the overridden terrorizeTown() method, the population of myTown remains 5922 and isn’t decreased by 10 as it should be. I’m confused as to why? Xcode version 7.2 doesn’t indicate any errors.


#2

Alright now I’m even more confused. When Town was a struct, town?.changePopulation(-10) in the Zombie class did nothing. Population was still always 5922.
I made Town a class. Once I did that, the value was changed with the overridden method in the Zombie class but not to 5912.

override func terrorizeTown() {
town?.changePopulation(-10)
}

This results in the following Console output:

Population: 5922, number of stoplights: 4
Monster is terrorizing a town!
Population: 5902, number of stoplights: 4

Now if I change the overridden method to:

override func terrorizeTown() {
town?.changePopulation(-0)
}

The Console output becomes:

Population: 5922, number of stoplights: 4
Monster is terrorizing a town!
Population: 5912, number of stoplights: 4

Why does this work and not the code in the original listing?


#3

Hi, if you do the math, it looks like there is somewhere in your code, that it is doing an extra changePopulation(-10)


#4

having the same problem here… trying to figure out a fix !


#5

the problem seems to be that the zombie class isn’t feeding back into the town struct somehow.

i tried changing town to a class as well and that did work. just have to remove the mutating keyword,
but that isn’t a fix.

i tried the following:

in Zombie.swift

override func terrorizeTown() { town?.changePopulation(-10) super.terrorizeTown() print("zombie population = \(town!.population)") }

this prints an extra population number to the console - but when the func is run

result:

Monster is terrorizing a town zombie population = 5490 Population = 5500, number of Stoplights = 4. Program ended with exit code: 0

so the population is being decreased by the call

town?.changePopulation(-10)
but for some reason (yet unknown) it isn’t updating when the super.terrorizeTown() line is run…


#6

oh - i changed the initial population to 5000 just to keep things easier to read…


#7

zombie population = 5490 Population = 5500, number of Stoplights = 4.

it’s late and i’m tired but i think the above proves that somehow, there is more than one instance of town happening here…


#8

aghhhhhh i found the problem !
the book tells us to write out the print town description first in the main.swift file.
then it says - move it to be embedded in the town struct itself.

i (stupidly) copied and pasted the whole print line in as it was:

print("Population = \(myTown.population), number of Stoplights = \(myTown.numberOfStoplights).")

of course the myTown. part should not be in there anymore, because myTown is the struct itself…
removing those ‘myTowns’ makes the code in the book work fine… !

print("Population = \(population), number of Stoplights = \(numberOfStoplights).")


#9

[quote=“Squillop”]aghhhhhh i found the problem !
the book tells us to write out the print town description first in the main.swift file.
then it says - move it to be embedded in the town struct itself.

i (stupidly) copied and pasted the whole print line in as it was:

print("Population = \(myTown.population), number of Stoplights = \(myTown.numberOfStoplights).")

of course the myTown. part should not be in there anymore, because myTown is the struct itself…
removing those ‘myTowns’ makes the code in the book work fine… !

print("Population = \(population), number of Stoplights = \(numberOfStoplights).") [/quote]

That worked! I hope the book explains more about that because I can’t figure out why the first version didn’t work.


#10

Thank you! I did the same an included the ‘myTown’ property within Town’s method via copy and paste. Worked as expected once removed. Much appreciated! :smile:


#11

Thank you! I had the same issue myself. Your post made take a closer look and found the problem :grin:


#12

Ok, so I wasn’t alone in doing the exact same thing you guys did by copying and pasting the town description with the “myTown” instance!
I’m glad I signed into the forum otherwise I may not have ever found the error that was right under my nose!
I’ll chalk it up as a good learning experience and keep moving forward!


#13

Thanks for the posting your find of the solution…I too made the same mistake!


#14

Many thanks!
I racked my brain for hours and almost gave up until finding this.


#15

Yes, thank you for pointing this out! Can’t believe I didn’t see this! Details, details, details! Works fine now.