Why doesn't Swift force my designated initializer to be called super?

This code is legal in Swift:

class Snapper : NSObject {
    var anim : UIDynamicAnimator
    init(referenceView:UIView) {
        self.anim = UIDynamicAnimator(referenceView:referenceView)
        // super.init()
    }
}

      

Note that in my initializer I did not call super.init()

; I have commented out this line. But the Swift compiler doesn't complain. What for? I thought there was a rule that a designated initializer should call its superclass's designated initializer. And I have a superclass, namely NSObject.

This is mistake? Or has NSObject as your superclass - a special case? If so, why? I understand that NSObject does not have instance variables that need to be initialized, but how do we know that it is init

not doing other things that need to be done? Shouldn't Swift be giving a compile error here?

+3


source to share


1 answer


This is not an answer to the question of why adding a symbolic breakpoint does not [NSObject init]

indicate that it is being called, even if super.init()

commented out.



This definitely seems like a special case, as replacing NSObject

with any other class causes the compiler to warn again about the missing supercall. I assume the compiler is treating this as a special case given that this is our base class with one known designated initializer.

+3


source







All Articles