Bronze, Silver, & Gold Challenges - Ch 16

You should cut & paste your code into your post rather than posting a photo of it.

The code in your photo looks OK. Either you forgot to set the mayor property in your town, or there’s something wrong with the code in mayor.mayorStatement().

Sorry @JonAult I’m new to this forum, my mayor code was like this.

struct Mayor {
mayorStatement() {
print(“I’m deeply saddened to hear about this latest tradgedy. I promise that my office is looking into the nature of this rash of violence.”)
}
}
I make the mayor type optional, but nothing showing in the console. Any guess?

That looks OK also.

So I’m guessing the problem is that you never set the mayor property in your town. In main.swift, you should have something like

var myTown = Town()
myTown.mayor = Mayor()

Without that second line, the mayor property in myTown is nil, and mayorStatement() never gets called when myTown’s population is changed.

This would be my guess as well.

Hey thx, when I try in playground the value was nil. I was thinking the same thing like you @JonAult but I never thought it’s should be initialize or called in main.swift

My Bronze Challenge (this will look familiar :wink: ):

struct Town {
    var mayor: Mayor?
    static let region = "South"
    var population = 5422 {
        didSet(oldPopulation) {
            if population < oldPopulation {
                print("The population has changed to \(population) from \(oldPopulation)")
            }
        }
    }

Here is my silver challenge:

struct Town {
    var mayor: Mayor?
    static let region = "South"
    var population = 5422 {
        didSet(oldPopulation) {
            if population < oldPopulation {
                print("The population has changed to \(population) from \(oldPopulation)")
                mayor?.cannedResponse()
            }
        }
    }

struct Mayor {
    func cannedResponse() {
        print("I'm deeply saddened to hear about this latest tragedy. I promise the my office is looking into the nature of this rash of violence.")
    }
}

Here is my gold challenge (I removed the unnecessary stuff from Zombie()):

struct Mayor {
    private var anxietyLevel = 0
    
    func cannedResponse() {
        print("I'm deeply saddened to hear about this latest tragedy. I promise the my office is looking into the nature of this rash of violence.")
    }
    
    mutating func raiseAnxietyLevel() {
        anxietyLevel += 1
    }
}

class Zombie: Monster {
    final override func terrorizeTown() {
        town?.mayor?.raiseAnxietyLevel()
        if !isFallingApart {
            town?.changePopulation(by: -10)
        }
        super.terrorizeTown()
    }    
}

Here is my Bronze, Silver, & Gold Challenge solution:

//  main.swift
var myTown = Town()
myTown.mayor = Mayor()
...
//  Town.swift
struct Town {
  var population = 5_422 {
    didSet(oldPopulation) {
      if population < oldPopulation {
        print("The population has changed to \(population) from \(oldPopulation).")
        mayor?.populationDecreased()
      }
    }
  }

  var mayor: Mayor?

  enum State {
    case safe
    case underZombieAttack
  }
  var state: State = .safe {
    didSet {
      if state == .underZombieAttack {
        self.mayor?.townUnderZombieAttack()
      }
    }
  }
...
}
//  Monster.swift
class Monster {
  func terrorizeTown() {
    if town != nil {
      print("\(name) is terrorizing a town!")
      town?.state = .underZombieAttack
    } else {
      print("\(name) hasn't found a town to terrorize yet...")
    }
  }
...
}
//  Mayor.swift
struct Mayor {
  private var anxietyLevel = 0

  mutating func townUnderZombieAttack() {
    anxietyLevel += 1
    print("Mayor anxietyLevel: \(anxietyLevel)")
  }

  func populationDecreased() {
    print("I'm deeply saddened to hear about this latest tragedy. I promise that my office is looking into the nature of this rash of violence.")
  }
}

I’m not sure it’s right, but it looks ok

so …

silver and gold

Mayor.swift

struct Mayor{
private var anxietyLevel = 0;
func VictimStatement(){
print("…")
}
mutating func anxieLevels(){
anxietyLevel += 1;
//print(anxietyLevel)
}
}
//My idea is to have another function call it to do the autoincrement,It really did what I expected,but,If mian.swift Execute it code onther paragraph,The anxietyLevel will be printed out

Town.swift
var mayor = Mayor()
var population = 5_244{
didSet(oldValue){
if oldValue > population{
mayor.VictimStatment()
}
}
}

Zombie.swift
var mayor = Mayor()
if !town!.population > 10{
town.?changePopulation(by: -10)
mayor.anxieLecel()
}