https://github.com/below/codersample
A Question About Swift and NSCoding
https://github.com/below/codersample
Last synced: about 1 year ago
JSON representation
A Question About Swift and NSCoding
- Host: GitHub
- URL: https://github.com/below/codersample
- Owner: below
- License: mit
- Created: 2017-06-11T07:35:05.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-12T11:55:14.000Z (almost 9 years ago)
- Last Synced: 2025-01-26T02:44:22.331Z (about 1 year ago)
- Language: Swift
- Size: 8.79 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# A Question About Swift and NSCoding
## Solved:
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.
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID216
It appears that when a class adheres to the `NSCoding` protocol, it also **must** have an `init` method even when it is, say, inheriting from `NSObject`
Why is that the case?
Providing boilerplate code will make the compiler happy:
```swift
override init () {
super.init()
}
```
This looks like syntactic sugar to me, and I don't expect this in Swift: An `init` method exists, apparent by the `override` keyword, and my method does nothing but call `super`. Yet, it is necessary.
What am I missing? Or is this a bug?
Oh, yes both Swift 3 and 4