@@ -925,9 +925,14 @@ convenience init(<#parameters#>) {
925925To simplify the relationships between designated and convenience initializers,
926926Swift applies the following three rules for delegation calls between initializers:
927927
928- - term ** Rule 1** : A designated initializer must call a designated initializer from its immediate superclass.
929- - term ** Rule 2** : A convenience initializer must call another initializer from the * same* class.
930- - term ** Rule 3** : A convenience initializer must ultimately call a designated initializer.
928+ - term ** Rule 1** :
929+ A designated initializer must call a designated initializer from its immediate superclass.
930+
931+ - term ** Rule 2** :
932+ A convenience initializer must call another initializer from the * same* class.
933+
934+ - term ** Rule 3** :
935+ A convenience initializer must ultimately call a designated initializer.
931936
932937A simple way to remember this is:
933938
@@ -990,28 +995,34 @@ by another initializer unexpectedly.
990995Swift's compiler performs four helpful safety-checks to make sure that
991996two-phase initialization is completed without error:
992997
993- - term ** Safety check 1** : A designated initializer must ensure that all of the properties introduced by its class
994- are initialized before it delegates up to a superclass initializer.
998+ - term ** Safety check 1** :
999+ A designated initializer must ensure that all of the properties introduced by its class
1000+ are initialized before it delegates up to a superclass initializer.
9951001
9961002As mentioned above,
9971003the memory for an object is only considered fully initialized
9981004once the initial state of all of its stored properties is known.
9991005In order for this rule to be satisfied, a designated initializer must make sure that
10001006all of its own properties are initialized before it hands off up the chain.
10011007
1002- - term ** Safety check 2** : A designated initializer must delegate up to a superclass initializer
1003- before assigning a value to an inherited property.
1004- If it doesn't, the new value the designated initializer assigns
1005- will be overwritten by the superclass as part of its own initialization.
1006- - term ** Safety check 3** : A convenience initializer must delegate to another initializer
1007- before assigning a value to * any* property
1008- (including properties defined by the same class).
1009- If it doesn't, the new value the convenience initializer assigns
1010- will be overwritten by its own class's designated initializer.
1011- - term ** Safety check 4** : An initializer can't call any instance methods,
1012- read the values of any instance properties,
1013- or refer to ` self ` as a value
1014- until after the first phase of initialization is complete.
1008+ - term ** Safety check 2** :
1009+ A designated initializer must delegate up to a superclass initializer
1010+ before assigning a value to an inherited property.
1011+ If it doesn't, the new value the designated initializer assigns
1012+ will be overwritten by the superclass as part of its own initialization.
1013+
1014+ - term ** Safety check 3** :
1015+ A convenience initializer must delegate to another initializer
1016+ before assigning a value to * any* property
1017+ (including properties defined by the same class).
1018+ If it doesn't, the new value the convenience initializer assigns
1019+ will be overwritten by its own class's designated initializer.
1020+
1021+ - term ** Safety check 4** :
1022+ An initializer can't call any instance methods,
1023+ read the values of any instance properties,
1024+ or refer to ` self ` as a value
1025+ until after the first phase of initialization is complete.
10151026
10161027The class instance isn't fully valid until the first phase ends.
10171028Properties can only be accessed, and methods can only be called,
@@ -1431,13 +1442,16 @@ and can inherit your superclass initializers with minimal effort whenever it's s
14311442Assuming that you provide default values for any new properties you introduce in a subclass,
14321443the following two rules apply:
14331444
1434- - term ** Rule 1** : If your subclass doesn't define any designated initializers,
1435- it automatically inherits all of its superclass designated initializers.
1436- - term ** Rule 2** : If your subclass provides an implementation of
1437- * all* of its superclass designated initializers ---
1438- either by inheriting them as per rule 1,
1439- or by providing a custom implementation as part of its definition ---
1440- then it automatically inherits all of the superclass convenience initializers.
1445+ - term ** Rule 1** :
1446+ If your subclass doesn't define any designated initializers,
1447+ it automatically inherits all of its superclass designated initializers.
1448+
1449+ - term ** Rule 2** :
1450+ If your subclass provides an implementation of
1451+ * all* of its superclass designated initializers ---
1452+ either by inheriting them as per rule 1,
1453+ or by providing a custom implementation as part of its definition ---
1454+ then it automatically inherits all of the superclass convenience initializers.
14411455
14421456These rules apply even if your subclass adds further convenience initializers.
14431457
0 commit comments