Bronze, Silver, & Gold Challenges - Ch 16


#22

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().


#23

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?


#24

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.


#25

This would be my guess as well.


#26

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


#27

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()
    }    
}

#28

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.")
  }
}