Properties are a little more straightforward for me to understand, so let’s see how I did with these challenges.
Bronze Challenge:
Simple change for this one, just need a if
statement before printing out a message:
/// Town.swift
var population: UInt = 5422 {
didSet(oldPopulation) {
if oldPopulation > population {
print("The population has changed to \(population) from \(oldPopulation)".uppercased())
}
}
}
Silver Challenge:
This one seems deceptively straightforward. All I did was create the Mayor
struct, gave it a method to print out the statement, and then called that statement in the didSet
in Town
’s population
.
/// Mayor.swift
struct Mayor {
func offerCondolences() {
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
var mayor = Mayor()
var population: UInt = 5422 {
didSet(oldPopulation) {
if oldPopulation > population {
print("The population has changed to \(population) from \(oldPopulation)".uppercased())
mayor.offerCondolences()
}
}
}
Gold Challenge
To specify which monster was doing the attacking, I specified an enum called AttackType
to represent which monster was currently attacking. Each monster registers itself as the attacker when it terrorizes the town, overwriting the property defined in the Town
struct. I modified the offerCondolences()
method, which I’ll explain afterwards:
/// Town.swift
struct Town {
....
var population: UInt = 5422 {
didSet(oldPopulation) {
if oldPopulation > population {
print("The population has changed to \(population) from \(oldPopulation)".uppercased())
mayor.offerCondolences(forMonsterAttackType: attackType)
}
}
}
....
var attackType: AttackType = .monster
var mayor = Mayor()
enum AttackType {
case zombie
case vampire
case monster
}
...
}
Go into the respective monster class files and register their attack type when terrorizing:
/// Zombie.swift
final override func terrorizeTown() {
...
town?.attackType = .zombie
....
}
/// Vampire.swift (I'm continuing from last chapter)
final override func terrorizeTown() {
...
town?.attackType = .vampire
....
}
Now for the mayor: instantiate his anxiety level, and add a parameter to pass in the attack type. Now you can monitor what type of attack is going on, and can increase the mayor’s anxiety level accordingly. You have to specify Town.AttackType
because the enum is nested inside of the Town
struct. I print the anxiety level simply because I wanted to make sure it was called and I can monitor its value in the console, because the private
keyword won’t allow me to access it from the main.swift
file. Also make sure the function is mutating
, since you’re modifying a property of the struct!
/// Mayor.swift
struct Mayor {
private var anxietyLevel: Int = 0
mutating func offerCondolences(forMonsterAttackType type: Town.AttackType) {
print("I'm deeply saddened to hear about this latest tragedy regarding the \(type)s. I promise that my office is looking into the nature of this rash of violence.")
if type == Town.AttackType.zombie {
anxietyLevel += 1
print("Current anxiety: \(anxietyLevel)")
}
}
}
After all that, my output comes to something like this:
- Zombie Fred is terrorizing a town!
- THE POPULATION HAS CHANGED TO 1005412 FROM 1005422
- I’m deeply saddened to hear about this latest tragedy regarding the zombies. I promise that my office is looking into the nature of this rash of violence.
- Current anxiety: 1
- Zombie Fred is terrorizing a town!
- THE POPULATION HAS CHANGED TO 1005402 FROM 1005412
- I’m deeply saddened to hear about this latest tragedy regarding the zombies. I promise that my office is looking into the nature of this rash of violence.
- Current anxiety: 2
- Zombie Fred is terrorizing a town!
- THE POPULATION HAS CHANGED TO 1005392 FROM 1005402
- I’m deeply saddened to hear about this latest tragedy regarding the zombies. I promise that my office is looking into the nature of this rash of violence.
- Current anxiety: 3
- Zombie Fred is terrorizing a town!
- THE POPULATION HAS CHANGED TO 1005382 FROM 1005392
- I’m deeply saddened to hear about this latest tragedy regarding the zombies. I promise that my office is looking into the nature of this rash of violence.
- Current anxiety: 4
- Population: 1005382, number of stoplights: 4
I’d love to heard thoughts/ideas on this one. I wanted to restrain myself from skipping ahead, because a lot of the problems I was having trying to solve this could’ve been easily solved with initializers (the next chapter topic). Wanted to keep myself honest