Swift Subclasses of Objective-C Classes Cannot Delegate to Inherited Convenience Initializers
| Originator: | wetzeal | ||
| Number: | rdar://17424482 | Date Originated: | 23-Jun-2014 |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | 6.0 (6A216f) |
| Classification: | Serious Bug | Reproducible: | Always |
Summary:
Examine the following swift class:
class MyDictionary : NSDictionary {
convenience init(beAwesome:Bool) {
if (beAwesome) {
self.init(object: true, forKey:"awesome")
} else {
self.init(object: false, forKey:"awesome")
}
}
}
This class does not compile. However, if the class inherits from this Swift class:
class MyClass {
var object: AnyObject!
var key: NSCopying!
convenience init(object: AnyObject!, forKey key: NSCopying!) {
self.init()
self.object = object
self.key = key
}
}
It works perfectly. It appears the Swift compiler is handling the inheritance of NSDictionary's convenience and delegated initializers differently than it handles Swift classes. This same error occurs for other Objective-C classes as well.
Steps to Reproduce:
1. Paste the first class into an Xcode playground
2. Open the assistant editor
3. Notice that the code didn't compile, and spit out a bunch of errors into the console output
Expected Results:
The code compiles
Actual Results:
The following error is printed:
Playground execution failed: error: <REPL>:8:13: error: use of 'self' in delegating initializer before self.init is called
self.init(object: true, forKey:"awesome")
^
<REPL>:8:18: error: use of 'self' in delegating initializer before self.init is called
self.init(object: true, forKey:"awesome")
^
<REPL>:10:13: error: use of 'self' in delegating initializer before self.init is called
self.init(object: false, forKey:"awesome")
^
<REPL>:10:18: error: use of 'self' in delegating initializer before self.init is called
self.init(object: false, forKey:"awesome")
^
<REPL>:12:5: error: self.init isn't called on all paths in delegating initializer
}
^
Version:
6.0/6A216f 10.9.3/13D65
Notes:
I've asked this question on Stack Overflow as well: http://stackoverflow.com/q/24373142
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!