43

I am attempting to create an access method to a singleton. I am getting this error (see code below). I don't understand why I am getting this error and what this error means. Can anyone explain?

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  static private var thedelegate: AppDelegate?

  class var delegate : AppDelegate {
    return thedelegate!
  }

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    thedelegate = self // syntax error: Static member 'thedelegate' cannot be used on instance of type 'AppDelegate'
andrewz
  • 4,729
  • 5
  • 49
  • 67

2 Answers2

43

You need to prepend the static/class variable or function with the class name in which it is declared, even when in that same class.

In this case, you want to return AppDelegate.thedelegate!

and, as Martin R. points out, AppDelegate.thedelegate = self

bjc
  • 1,201
  • 12
  • 10
  • 5
    It seems inconsistent because if you do a let x = someStatic it works in the class's global scope, no need for the prefix. – LegendLength Apr 07 '17 at 12:17
  • 2
    @LegendLength few languages seem to make a lot of sense. `swift` actually does better than many (though questionable here ;) ) – WestCoastProjects May 16 '20 at 23:25
28

You are trying to access Class level variable from the instance of that class. To use that you need to make Class level function: static func (). Try this:

static func sharedDelegate() -> AppDelegate {
    return UIApplication.sharedApplication().delegate as! AppDelegate
}
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
Sasha Kozachuk
  • 1,283
  • 3
  • 14
  • 21
  • 27
    "You are trying to access Class level parameter from instance" - why can't I do so? – andrewz Mar 09 '16 at 18:45
  • check out this answer - http://stackoverflow.com/questions/29636633/static-vs-class-functions-variables-in-swift-classes – Sasha Kozachuk Mar 09 '16 at 18:53
  • 2
    hm, I did, but it doesn't answer my question. It talks about overriding static vs class methods/properties. I'm not trying to override a property. I am trying to access a static property ('thedelegate') from within an instance's method. – andrewz Mar 09 '16 at 18:57
  • 7
    @andrewz static associate a variable with a class, not an instance of class. When you try: thedelegate = self. Compiler tries to access self.thedelegate. But it is not there, if it were there would possibly be many different thedelegates. Static directive is exactly opposite, so we could have only value for that variable for whole Class. Hope that helps. – Sasha Kozachuk Mar 09 '16 at 19:15
  • 14
    ah, so I simply didn't prefix the class var with the class name, ie AppDelegate.thedelegate = self. Coming from C++ will mess with your head. – andrewz Mar 09 '16 at 19:25
  • Too bad you can't say AppDelegate::thedelegate. :) This warning message is very misleading "Static member 'staticmbr' cannot be used on instance of type 'classname'". Most Java devs also probably find this confusing. – paiego May 01 '20 at 21:25