GithubHelp home page GithubHelp logo

语言特性 about aswifttour HOT 10 OPEN

ShannonChenCHN avatar ShannonChenCHN commented on July 21, 2024
语言特性

from aswifttour.

Comments (10)

ShannonChenCHN avatar ShannonChenCHN commented on July 21, 2024

KVC & KVO

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

from aswifttour.

ShannonChenCHN avatar ShannonChenCHN commented on July 21, 2024

Access Level

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

from aswifttour.

ShannonChenCHN avatar ShannonChenCHN commented on July 21, 2024

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

from aswifttour.

ShannonChenCHN avatar ShannonChenCHN commented on July 21, 2024

泛型

from aswifttour.

ShannonChenCHN avatar ShannonChenCHN commented on July 21, 2024

NSCopying

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

from aswifttour.

ShannonChenCHN avatar ShannonChenCHN commented on July 21, 2024

Subscripts

from aswifttour.

ShannonChenCHN avatar ShannonChenCHN commented on July 21, 2024

其他

from aswifttour.

ShannonChenCHN avatar ShannonChenCHN commented on July 21, 2024

Protocol

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

from aswifttour.

ShannonChenCHN avatar ShannonChenCHN commented on July 21, 2024

Hashable

from aswifttour.

ShannonChenCHN avatar ShannonChenCHN commented on July 21, 2024

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.

参考

from aswifttour.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.