ShakeAnimation
ShakeAnimation

Reputation: 3

swift calls self before super.init

Access self before super.init()call is forbid according to the document.Why use self.properties is correct.

var myString: String
init(frame: CGFloat) {
    //error:'self' userd before super.init call
    print(self)
    self.myString = "11"
    print(self.myString)

    super.init()
}

Why It occurs an error calls print(self),calls print(self.myString)is ok?

Upvotes: 0

Views: 351

Answers (1)

impression7vx
impression7vx

Reputation: 1863

The reasoning is logical context, let's walk through this.

In the line self.myString = "11", you declare what myString is, which is == "11".

Now, you could print(self.myString).

But! You canNOT print self, because SELF was never initialized/declared! You must call SOME kind of initializer, i.e. the parent initializer (super.init) and then you declare/instantiate some type of object.

So you may not necessarily know what SELF is, but you can definitely determine what self.myString is!

Upvotes: 1

Related Questions