Comments (10)
KVC & KVO
https://medium.com/hackernoon/kvo-kvc-in-swift-12f77300c387
from aswifttour.
Access Level
https://useyourloaf.com/blog/swift-4-access-levels/
https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html
from aswifttour.
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.
泛型
- Gentle Generics – John Sundell at Hacking with Swift Live 2019
- Generic Swift: It Isn’t Supposed to Hurt - Rob Napier
- 2017 LLVM Developers’ Meeting: “Implementing Swift Generics ”
from aswifttour.
NSCopying
https://swiftrocks.com/nscopying-nszone-uses-in-swift.html
from aswifttour.
Subscripts
from aswifttour.
其他
from aswifttour.
Protocol
https://www.swiftbysundell.com/articles/different-categories-of-swift-protocols/
from aswifttour.
Hashable
from aswifttour.
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.
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)
- Swift-ObjC 混编 HOT 6
- SwiftUI HOT 1
- 值类型(struct) VS. 引用类型(class) HOT 2
- WWDC(Swift 相关) HOT 1
- Swift 5.x HOT 3
- Swift 编译 HOT 1
- Swift 性能 HOT 2
- Swift Memory Layout 和 Runtime HOT 3
- Swift Package Manager HOT 1
- 最佳实践(Best Practices) HOT 1
- Debugging HOT 1
- Talks, posts and questions HOT 3
- JSON 解析
- OC和siwft混编,脚本生成XXX-swift.h,脚本报错
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aswifttour.