ASwiftTour icon indicating copy to clipboard operation
ASwiftTour copied to clipboard

语言特性

Open ShannonChenCHN opened this issue 6 years ago • 10 comments

ShannonChenCHN avatar Mar 05 '20 00:03 ShannonChenCHN

KVC & KVO

https://medium.com/hackernoon/kvo-kvc-in-swift-12f77300c387

ShannonChenCHN avatar Mar 05 '20 00:03 ShannonChenCHN

Access Level

https://useyourloaf.com/blog/swift-4-access-levels/ https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html

ShannonChenCHN avatar Mar 05 '20 00:03 ShannonChenCHN

Property Wrapper

示例代码

参考资料

https://www.raywenderlich.com/4187396-what-s-new-in-swift-5-1 https://medium.com/genetec-tech/property-wrappers-in-swift-5-1-the-missing-published-implementation-1a466ebcf660 https://nshipster.com/propertywrapper/ https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md https://medium.com/flawless-app-stories/turning-property-wrappers-into-function-wrappers-2be3a49229f5

ShannonChenCHN avatar Mar 05 '20 00:03 ShannonChenCHN

NSCopying

https://swiftrocks.com/nscopying-nszone-uses-in-swift.html

ShannonChenCHN avatar Mar 05 '20 01:03 ShannonChenCHN

Protocol

https://www.swiftbysundell.com/articles/different-categories-of-swift-protocols/

ShannonChenCHN avatar May 19 '20 07:05 ShannonChenCHN

Swift Initializer

1. Initializer Delegation for Class Types

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.

image

2. Safety Check

Swift’s compiler performs four helpful safety-checks to make sure that two-phase initialization is completed without error:

Safety check 1

A 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 2

A 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 3

A 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 4

An 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 Initialization

Here’s how two-phase initialization plays out, based on the four safety checks above:

Phase 1

A designated or convenience initializer is called on a class. Memory for a new instance of that class is allocated. The memory is not yet initialized. A designated initializer for that class confirms that all stored properties introduced by that class have a value. The memory for these stored properties is now initialized. The designated initializer hands off to a superclass initializer to perform the same task for its own stored properties. This continues up the class inheritance chain until the top of the chain is reached. Once the top of the chain is reached, and the final class in the chain has ensured that all of its stored properties have a value, the instance’s memory is considered to be fully initialized, and phase 1 is complete.

Phase 2

Working 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. Finally, any convenience initializers in the chain have the option to customize the instance and to work with self.

4. Initializer Inheritance and Overriding

Unlike subclasses in Objective-C, Swift subclasses do not inherit their superclass initializers by default.

Why?

Swift’s approach prevents a situation in which a simple initializer from a superclass is inherited by a more specialized subclass and is used to create a new instance of the subclass that is not fully or correctly initialized.

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 Inheritance

Assuming that you provide default values for any new properties you introduce in a subclass, the following two rules apply:

Rule 1

If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.

Rule 2

If 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.

参考

ShannonChenCHN avatar Jun 15 '20 03:06 ShannonChenCHN