5

Why isn't it permitted to delegate from a designated initializer in a subclass to a convenience initializer of its superclass?

I'm well aware that as per documentation a designated initializer in a subclass must delegate up to a designated initializer of its superclass.

Initializer Delegation for Class Types

To simplify the relationships between designated and convenience initializers, Swift applies the following three rules for delegation calls between initializers:

Rule 1

A designated initializer must call a designated initializer from its immediate superclass.

Rule 2

A convenience initializer must call another initializer from the same class.

Rule 3

A convenience initializer must ultimately call a designated initializer.

A simple way to remember this is:

  • Designated initializers must always delegate up.

  • Convenience initializers must always delegate across.

I also understand the difference between designated and convenience initializer as well as the two-phase initialization process, but wouldn’t calling a convenience initializer of the superclass which in turn leads to a call of a corresponding designated initializer in the same superclass (compare chart) satisfy the requirement of fully initialized properties throughout class hierarchy?

Even if there are only minor benefits of calling convenience initializer directly, I'm still wondering whether there are good reasons for not allowing this (e.g. related to inheritance or overriding)?

Thank you in advance.


Chart: Calling a convenience initializer of superclass directly

Calling a convenience initializer of superclass directly.

Tobi
  • 674
  • 6
  • 12
  • 1
    possible duplicate: http://stackoverflow.com/questions/24122421/why-cant-swift-initializers-call-convenience-initializers-on-their-superclass – Philip Frank Sep 06 '16 at 11:00
  • You're right, I'm afraid I didn't find this question while looking for answers - unfortunately it doesn't provide a satisfying answer either. – Tobi Sep 07 '16 at 11:21

0 Answers0