Silver Challenge


#1

Is there a reason why I am not able to define the mayor var on the town as a Mayor? optional? If I do and I assign it a new mayor in main, it still has nil as the mayor variable in the observer call inside the population

// Town.swift
...
    var mayor: Mayor?
    var numberOfStoplights = 4
    var population = 5422 {
        didSet(oldPopulation) {
            if population < oldPopulation {
                print("The population has changed to \(population) from \(oldPopulation)")
                mayor?.response()
            }
        }
    }
...

// Mayor.swift
struct Mayor {
    
    func response() {
        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.")
    }
}

// main.swift
...
let mcCheese = Mayor()
myTown.mayor = mcCheese

fredTheZombie.terrorizeTown()
...

it will work if I set the var mayor = Mayor() in town.


#2

In order to get this to work you have to refer to self.mayor?.response. I thought that I had tried that and it didn’t work but now it seems to be working.


#3

I think your instance for the Mayor should be called with var mayor = Mayor() . If the town has the instance you shouldn’t need to assign him in main.swift, he’s already created in your town instance myTown.


#4

Mayor.swift

struct Mayor {
    var name = "Swifty Swiftington"
    
    func issueStatement() {
        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.")
    }
}

Town.swift

struct Town {
    var population: Int = 5422 {
        didSet(oldPopulation) {
            if population < oldPopulation {
                print("The population has decreased to \(population) from \(oldPopulation).")
                mayor.issueStatement()
            }
        }
     }
     var mayor = Mayor()
}

Main.swift

var myTown = Town()
myTown.changePopulation(by: -10)  
// Prints "The population has decreased to 5412 from 5422." and also prints "I'm deeply saddened to hear..."

I combined the Bronze Challenge and Silver Challenge into the same Property Observer in the Town struct definition.


#5

import Foundation

struct Town {

let region = "South"

var mayor = Mayor()

var population = 5422 {
    didSet(oldPopulation) {
        if oldPopulation > population {   // Bronze Challenge: Add an if statement to only log changes to the town's population if the new population value is less than the old value.
            print("The population has changed to \(population) from \(oldPopulation).")
            informMayor()
            mayor.issueStatement()
        } else {
            mayor.doNothing()
        }
    }
}

var numberOfStoplights = 4

enum Size {
    case small
    case medium
    case large
}


var townSize: Size {    // COMPUTED PROPERTY:
    get {
        switch self.population {
        case 0...10000:
            return Size.small
        case 10001...100000:
            return Size.medium
        default:
            return Size.large
        }
    }
}

func printDescription() {
    print("Population: \(population); number of stoplights: \(numberOfStoplights)")
}

func informMayor() {
    print("Town to Mayor: 'Mr Mayor, this is to inform you that the town's population has changed.'".uppercased())
}

mutating func changePopulation(by amount: Int) {
    population += amount
}

mutating func changeNumberOfStoplights(by number: Int) {
    numberOfStoplights += number
}

}

struct Mayor {

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

func doNothing() {
    print("Thank God nobody died today. Mike, get me a soda!")
}

}

Console Output:

Population: 1005402; number of stoplights: 7

Victim pool: 1005392

The population has changed to 500 from 1005392.

TOWN TO MAYOR: 'MR MAYOR, THIS IS TO INFORM YOU THAT THE TOWN’S POPULATION HAS CHANGED.'

Mayor to Town: 'I am deeply saddened to hear about this latest tragedy. I promise that my office is looking into the nature of this rash of violence.'

Victim pool: 500.

Brains…

Run away!

Program ended with exit code: 0

Combined solution for the Bronze & Silver challenges… The Silver challenge says: ‘Have your town inform the mayor every time the property for population changes’, but most of the solutions listed here haven’t taken that into consideration. Please share your thoughts on how to improve this solution. (Posting on this forum for the 1st time… Sorry for the mess!)