UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Confusion on Static Properties and Methods Accessing Non-Static Properties and Methods

Forums > 100 Days of SwiftUI

Hi! Paul Hudson explained a small detail about static properties and methods (2:40 - 3:25) that I would like to clarify. He talked about how static properties and methods cannot access/use non-static properties and methods because it doesn't really make sense. I'm a bit confused about this.

Obviously, this will cause an error:

struct TaylorFan {
  var name: String
  var age: Int
  var favoriteSong: String

  static func dance() {
    print("Let's dance to \(favoriteSong)!") // Error: Instance member 'favoriteSong' cannot be used on type 'TaylorFan'
  }
}

This is a problem because I am using favoriteSong, a non-static property, in a static method that will be called by the TaylorFan type. Swift has no idea what instance to refer to in order to use favoriteSong, so an error makes sense here.

However, I'm getting confused when Paul Hudson described, "There's no way of saying, 'I want to refer to that instance over there.'" Can't I do exactly that by writing this code? This won't cause an error and this is using a non-static property within a static method:

struct TaylorFan {
  var name: String
  var age: Int
  var favoriteSong: String

  static func dance() {
    print("Let's dance to \(fan.favoriteSong)!") // No error: We're referring to the favoriteSong of fan
  }
}

let fan = TaylorFan(name: "James", age: 25, favoriteSong: "Shake It Off")
TaylorFan.dance() // This will print, "Let's dance to Shake It Off!"

Perhaps he was just talking about this in the context of the first section of code? I am literally using a non-static property within a static method in the second example.

In addition to this, I would like to double check that I have this correct:

  • Accessing non-static properties/methods from non-static properties/methods is allowed ✅
  • Accessing static properties/methods from static properties/methods is allowed ✅
  • Accessing static properties/methods from non-static properties/methods is allowed ✅
  • Accessing non-static properties/methods from static properties/methods is NOT allowed ❎

Thanks for the help! :)

1      

That only works because fan is a global variable. If it were scoped to a View or another struct or pretty much anything else, it wouldn't work.

And relying on a global variable like that breaks encapsulation. The methods (static or otherwise) of a struct or class should not have knowledge of global variables like that. Heck, you shouldn't even really use global variables in your code.

To be honest, it doesn't make a whole lot of sense for dance() to be a static method. TaylorFan is a template for creating fans, a description of an abstract fan, that only becomes an actual fan upon initializing a new instance. Can you make a template or an abstract fan dance? Or do you need an actual fan?

  • Accessing non-static properties/methods from non-static properties/methods is allowed ✅
  • Accessing static properties/methods from static properties/methods is allowed ✅
  • Accessing static properties/methods from non-static properties/methods is allowed ✅
  • Accessing non-static properties/methods from static properties/methods is NOT allowed ❎

Yes, that is correct.

1      

(edit: apologies to Patrick ... i found out i was was posting my response just a few minutes after he was completing his post -- he beat the clock on me!.)

hi,

Can't I do exactly that by writing this code? This won't cause an error and this is using a non-static property within a static method:

the reason that this code works:

let fan = TaylorFan(name: "James", age: 25, favoriteSong: "Shake It Off")
TaylorFan.dance() // This will print, "Let's dance to Shake It Off!"

is that fan is declared as a global ("at the top level of your code"), so the value of fan is available when you call TaylorFan.dance().

on the other hand, if you had a function such as

func printFanDance() {
  let fan = TaylorFan(name: "James", age: 25, favoriteSong: "Shake It Off")
  TaylorFan.dance() 
}

you have a problem, because fan is local to the function; it's not accessible to the static function Taylor.dance.

I would like to double check that I have this correct ...

i think you have this right, unless someone wants to parse your question more carefully than i have.

in effect, a static function is just a global function that lives in the namespace of the defining struct or class. if you define something like

class MyWhateverClass {
  static func doSomethingWith(_ x: Double) {
    print(x)
  }
}

then these two statement are effectively the same:

print(3.3)

and

MyWhateverClass.doSomethingWith(3.3)

i think that might help,

DMG

1      

Perhaps he was just talking about this in the context of the first section of code?

This must be the answer, then.

@roosterboy Thanks, my mistake! I should have thought about that example a bit better. That method probably wouldn't be actually marked as static in actual code.

@delawaremathguy Thank you for the help! I'm sorry I can't mark two answers as correct!

Really, I was just playing around and thought of this scenario.

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.