-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
语言特性 #8
Comments
Subscripts |
Swift Initializer1. Initializer Delegation for Class TypesRule 1A designated initializer must call a designated initializer from its immediate superclass. Rule 2A convenience initializer must call another initializer from the same class. Rule 3A convenience initializer must ultimately call a designated initializer. A simple way to remember this is:
2. Safety CheckSwift’s compiler performs four helpful safety-checks to make sure that two-phase initialization is completed without error: Safety check 1A designated initializer must ensure that all of the properties introduced by its class are initialized before it delegates up to a superclass initializer. Safety check 2A designated initializer must delegate up to a superclass initializer before assigning a value to an inherited property. If it doesn’t, the new value the designated initializer assigns will be overwritten by the superclass as part of its own initialization. Safety check 3A convenience initializer must delegate to another initializer before assigning a value to any property (including properties defined by the same class). If it doesn’t, the new value the convenience initializer assigns will be overwritten by its own class’s designated initializer. Safety check 4An initializer cannot call any instance methods, read the values of any instance properties, or refer to self as a value until after the first phase of initialization is complete. 3. Two-Phase InitializationHere’s how two-phase initialization plays out, based on the four safety checks above: Phase 1A designated or convenience initializer is called on a class. Phase 2Working back down from the top of the chain, each designated initializer in the chain has the option to customize the instance further. Initializers are now able to access self and can modify its properties, call its instance methods, and so on. 4. Initializer Inheritance and OverridingUnlike subclasses in Objective-C, Swift subclasses do not inherit their superclass initializers by default. Why?
If you want a custom subclass to present one or more of the same initializers as its superclass, you can provide a custom implementation of those initializers within the subclass. 5. Automatic Initializer InheritanceAssuming that you provide default values for any new properties you introduce in a subclass, the following two rules apply: Rule 1If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers. Rule 2If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers. 参考 |
No description provided.
The text was updated successfully, but these errors were encountered: