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?

3
  • 2
    This is an obsolete way of making a "singleton". Please follow the preferred way like here: stackoverflow.com/a/36012158/2227743 Commented Nov 22, 2016 at 10:04
  • 3
    Regarding your issue: your class does not inherit from anything, so why would you call super? It's not needed. Commented Nov 22, 2016 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. Commented Nov 22, 2016 at 10:07

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I changed my code to this: prntscr.com/dac7sw and accessed from another view controller with prntscr.com/dac851 But i couldn't see my print on log section. Where is the problem?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.