mixing objective-c and swift when subclassing

IosObjective CSwift

Ios Problem Overview


I've got a subclass of UIViewController written in Swift called 'BaseViewController'. I now have an 'old' view controller called 'ViewController1' written in Objective-C that I want to inherit from 'BaseViewController'. Following other advice I have imported the 'Project-Swift.h' header file.

My problem occurs when subclassing like this

#import <UIKit/UIKit.h>
#import "MyProject-Swift.h"

@interface ViewController1 : BaseViewController
@end

The error is:

Cannot subclass a class with objc_subclassing-restricted attribute

and it appears on the @interface ... line.

Ios Solutions


Solution 1 - Ios

Solution 2 - Ios

You cannot subclass a Swift class in Objective-C. cf. towards the end of this section of the docs: > However, note that you cannot subclass a Swift class in Objective-C.

Solution 3 - Ios

It's an compiling error, so U can not subclass a swift class in objc.

SWIFT_CLASS("_TtC6Swifty14ViewController")
@interface ViewController : UIViewController
@end


# if defined(__has_attribute) && __has_attribute(objc_subclassing_restricted)
#  define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) SWIFT_CLASS_EXTRA
#  define SWIFT_CLASS_NAMED(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA
# else
#  define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA
#  define SWIFT_CLASS_NAMED(SWIFT_NAME) SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA
# endif
#endif

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionapotryView Question on Stackoverflow
Solution 1 - IosAnil VargheseView Answer on Stackoverflow
Solution 2 - IosLukasView Answer on Stackoverflow
Solution 3 - Iossteven yuView Answer on Stackoverflow