user2924482
user2924482

Reputation: 9120

Swift: creating initializer generates this error: " 'super' members cannot be referenced in a root class"

I'm implementing a helper class for example:

class Helper {

    var myStr: String?

    init?(myStr:String) {
        super.init()
        self.myStr = myStr
    }
}

On this line super.init() I'm getting this error "'super' members cannot be referenced in a root class".

Any of you knows why I'm getting this error? or if there is a way for create the init without this error?

I'll really appreciate your help.

Upvotes: 3

Views: 4485

Answers (1)

Chris
Chris

Reputation: 4391

This is because this class has no superclass. It does not inherit from anything, and as such is a root class. A call to super.init() is for calling the initialiser of the superclass to ensure that inherited functionality is available as expected, but it has no relevance here.


Declaring a subclass that inherits from another class (a superclass) would look like this:

class Subclass: Superclass {
    // Code etc...
}

Upvotes: 6

Related Questions