2
class MissionControl {

    var nameField=""
    var surnameField=""
    var phoneField=""
    var tcIdField=""
    var photoField=""


    class var sharedInstance: MissionControl {
        struct Singleton { static let instance = MissionControl() }
        return Singleton.instance
    }



    override init() {
        super.init()
    }

}

I was using this class with Swift 2.3 and it was working well. But I couldn't use with Swift 3. I am getting following compiler errors:

Initializer does not override a designated initializer from its superclass

'super' members cannot be referenced in a root class

How can I fix them?

Tolgay Toklar
  • 4,151
  • 8
  • 43
  • 73
  • 2
    This is an obsolete way of making a "singleton". Please follow the preferred way like here: http://stackoverflow.com/a/36012158/2227743 – Eric Aya Nov 22 '16 at 10:04
  • 3
    Regarding your issue: your class does not inherit from anything, so why would you call super? It's not needed. – Eric Aya Nov 22 '16 at 10:05
  • Do you want to make it a singleton class? if yes, "init" should be private, also, there is a better way to do it. – Ahmad F Nov 22 '16 at 10:07

1 Answers1

7

You can fix this by not calling super.init(). Your class is not declared as inheriting from another base class so there is no super.init() to call.

By the way, for your shared instance I would just do

static let sharedInstance = MissionControl()
JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Ok, I changed my code to this: http://prntscr.com/dac7sw and accessed from another view controller with http://prntscr.com/dac851 But i couldn't see my print on log section. Where is the problem? – Tolgay Toklar Nov 22 '16 at 11:03
  • @TolgayToklar Not sure what the issue is but I think it happens lazily i.e. not until the shared instance is needed and if you don't use the shared instance, it may be that the `let` statement is being optimised out. Try not only doing an assign but also calling a method on sharedInstance. – JeremyP Nov 22 '16 at 11:42