A convenience initializer is a secondary, supporting initializer for a class that provides an alternative initialization path. It must strictly delegate initialization responsibility to a designated initializer within the exact same class before it is permitted to accessDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
self or modify any properties.
In Swift, the convenience modifier is exclusive to reference types (classes). Value types (structs and enums) can perform cross-delegation without this keyword.
Syntax
A convenience initializer is declared by placing theconvenience modifier immediately before the init keyword.
Delegation Rules
Swift enforces strict rules for class initialization to guarantee memory safety, commonly referred to as the Rule of Three:- Delegation Across: A convenience initializer must call another initializer from the same class using
self.init. It cannot call asuper.initdirectly. - Ultimate Resolution: A convenience initializer must ultimately resolve to a designated initializer. It can call another convenience initializer, but the chain of calls must end at a designated initializer.
- Designated Delegation Up: Only designated initializers are permitted to delegate up to a superclass initializer.
Two-Phase Initialization Constraints
Convenience initializers are bound by Swift’s two-phase initialization process.- Phase 1: The convenience initializer must delegate across (
self.init) before it can assign a value to any property, including properties defined in its own class. - Phase 2: Once the designated initializer completes the memory allocation and base initialization, control returns to the convenience initializer, which is then permitted to mutate properties or call instance methods.
Structural Example
The following code demonstrates the mechanical routing of convenience initializers without relying on a specific implementation use-case:Inheritance Behavior
Convenience initializers are not directly overridden by subclasses. However, they are subject to Swift’s automatic initializer inheritance rules:- If a subclass does not define any designated initializers, it automatically inherits all of its superclass designated and convenience initializers.
- If a subclass provides an implementation for all of its superclass’s designated initializers (either by inheriting them or overriding them), it automatically inherits all of the superclass’s convenience initializers.
Master Swift with Deep Grasping Methodology!Learn More





