在子类化时混合objective-c和swift。

时间:2022-09-07 07:41:24

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.

我有一个UIViewController的子类写在Swift中叫做'BaseViewController'我现在有一个名为“ViewController1”的“老”视图控制器,它写在Objective-C中,我想从“BaseViewController”继承。以下是我导入“项目- swift”的建议。h的头文件。

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

不能使用objc_subclass -受限属性来子类化一个类。

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

它出现在@interface上…线。

3 个解决方案

#1


84  

You cannot subclass a Swift class in Objective-C.

在Objective-C中不能子类化一个Swift类。

Excerpt from Using Swift with Cocoa and Objective-C

这是摘录自使用Swift与Cocoa和Objective-C。

#2


9  

You cannot subclass a Swift class in Objective-C. cf. towards the end of this section of the docs:

在Objective-C中不能子类化一个Swift类。cf在本部分的结尾部分:

However, note that you cannot subclass a Swift class in Objective-C.

但是,请注意,您不能在Objective-C中子类化一个Swift类。

#3


0  

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

这是一个编译错误,所以你不能在objc中子类化一个swift类。

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

#1


84  

You cannot subclass a Swift class in Objective-C.

在Objective-C中不能子类化一个Swift类。

Excerpt from Using Swift with Cocoa and Objective-C

这是摘录自使用Swift与Cocoa和Objective-C。

#2


9  

You cannot subclass a Swift class in Objective-C. cf. towards the end of this section of the docs:

在Objective-C中不能子类化一个Swift类。cf在本部分的结尾部分:

However, note that you cannot subclass a Swift class in Objective-C.

但是,请注意,您不能在Objective-C中子类化一个Swift类。

#3


0  

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

这是一个编译错误,所以你不能在objc中子类化一个swift类。

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