Why can't I . .

Trying to understand Type Properties a bit. So, fredTheMonster is an instance of Zombie. The example in the book provides that I can obtain the Zombie’s computed type property of “spookyNoise”:

Zombie.spookyNoise

So, Fred’s spooky noise is “Brains…”

But Fred is an instance of Zombie, so it seems as if I should be able to do this:

fredTheZombie.spookyNoise

Do that, and you get this compiler error: “Static member ‘spookyNoise’ cannot be used on instance of type ‘Zombie’”

I do not get that. . . .

Currently, the rules of the language bans that. I am pretty sure, its designers have good reasons for banning that.

why ?

You stated it right: “Fred is an instance of Zombie”

It’s for that very reason that you cannot to that!

You have a type (Zombie)

and you have instances of it (like Fred I guess)

every instance has got its own properties and methods

type properties and methods belong ONLY to the type itself.

If thoose props and methods were accessible also within instances… that would be misleading al least!

So, if you need them, you’ll specifically address them always with type name prefix like Zombie.spookynoise.

Again, it was meant to be that way. It’s not a missing feature.

Working on the silver challenge now,

Does anyone have any hints for how to use town and population in Mayor struct? Since they’re value types I know I can’t just use them in Mayor but not sure how to use them

What exactly are you trying to do?

If you can you elaborate on that, you will increase the likelihood of getting help.

@ibex10

You’re right, actually posting the issue would help. I’m trying to do the instance method for the Mayor struct. My way of thinking about doing this problem was…

  1. define a mutating method on a new structure called “Mayor” that takes an Int for the parameter and returns a string

  2. within this mutating method check that there is a town and that the town has a mayor by using an optional

  3. use if-else statements: if population decreases return the string and if population increases return/exit function

  4. then use this method in town.swift using

var mayor = Mayor()

  1. call in main.swift
  2. Really not understanding how to use town and population in Mayor struct since they’re both value types and can’t be inherited by the Mayor struct

Here’s the code I have for mayor.swift so far (I know it’s wrong…I keep adjusting):


//Mayor.swift

import Foundation

struct Mayor {
    
    mutating func informTheMayor(if popDec: Int) -> String {
        if let mayorOfTown = town?.population.mayor {
            if population -= 1 {
                return "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."
                else if population += 1 {
                  return
                }
            }
        }
    }
}

//Since Mayor is a struct and therefore a value type, how do I reference town and population?

Here’s the code for town.swift so far:


//town.swift

var mayor = Mayor()

The main issue I’m having is not being able to access the town struct and population property because they’re value types and can’t be used in Mayor struct. Any hints or suggestions are appreciated ?

I don’t have the book. But, even though this practice may raise eyebrows, you can always pass the instances of structs as inout parameters to where they are needed, which the following example illustrates.

struct ModifyStruct {
    struct Foo : CustomStringConvertible {
        var value = 0
        
        var description: String {
            return "Foo.value = \(value)"
        }
    }
    
    struct Bar {
        func modify (foo: inout Foo) {
            foo.value = 7
        }
    }
    
    static func main () {
        var foo = Foo ()
        let bar = Bar ()
        print (foo)
        bar.modify (foo: &foo)  // & means pass the corresponding arg as inout.
        print (foo)
    }
}

ModifyStruct.main ()

// Prints
Foo.value = 0
Foo.value = 7

Yeah it definitely did lol. I understand what you did conceptually by interchanging the foo.value but don’t think this is the best solution for me to try and implement right now.