New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-746] Objective-c subclass unimplemented initializer #43361
Comments
This is correct behavior. In Swift, a subclass is not required (or expected) to inherit its superclass's initializers, whereas in Objective-C it usually is. The best we can do is crash at runtime. …well, actually, I suppose we could do better by marking the initializer unavailable in the subclass in the generated header. |
Comment by Thomas Torp (JIRA) I expected it to be the correct behavior 🙂 But as you said it would be really great if the initializer is unavailable in the subclass 🙂 |
I think the generate header file should look like this: SWIFT_CLASS("_TtC17TestProject13SomeBaseClass")
@interface SomeBaseClass : NSObject
@property (nonatomic, readonly, copy) NSString * __nonnull type;
- (nonnull instancetype)init __unavailable;
- (nonnull instancetype)initWithType:(NSString * __nonnull)type OBJC_DESIGNATED_INITIALIZER;
@end
SWIFT_CLASS("_TtC17TestProject12SomeSubClass")
@interface SomeSubClass : SomeBaseClass
- (nonnull instancetype)init __unavailable;
- (nonnull instancetype)initWithType:(NSString * __nonnull)type __unavailable;
- (nonnull instancetype)initWithOtherValue:(NSString * __nonnull)otherValue OBJC_DESIGNATED_INITIALIZER;
@end |
Also every function that is marked as |
You can't mark something |
I agree with the general pattern, though I would add a message to the unavailable attribute. |
Yeah, sorry, I meant every initializer marked as |
What message should go with the unavailable attribute? I'm not able to come up with anything that adds anything meaningful beyond the default "'foo' is unavailable". We certainly can't suggest a different initializer because the compiler doesn't know what other initializer would be appropriate as a suggestion. |
Submitted as PR #3852. |
Comment by Thomas Torp (JIRA) Eridius (JIRA User) I feel that unavailable message is the right choice. Thanks for solving this issue 🙂 |
Merged in 0473e99. |
Additional Detail from JIRA
md5: 00cfa5be4152aa8f7bb3c422b0b71fb2
Issue Description:
When using swift code from objective-c the following code will crash runtime with this error:
"fatal error: use of unimplemented initializer 'init(type: )' for class 'TestProject.SomeSubClass'"
Generated -swift.h file:
This happens because both the parent and subclass init method is accessible from objective-c.
Possible solutions:
The text was updated successfully, but these errors were encountered: