从Objective C中的Swift类继承 [英] Inherit from a Swift class in Objective C

查看:206
本文介绍了从Objective C中的Swift类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xcode 7项目中成功地混合和匹配Obj-C和Swift。但是,我似乎无法弄清楚在Objective C类中如何从Swift类继承(是的,我知道将Swift类声明为 @objc 为了能见度)。在这种情况下,所需的Swift超类 MySwiftViewController UIViewController 的子类。现在,在Obj-C中,我直接从 UIViewController 继承,并且无法访问我在 MySwiftViewController中添加的功能

I'm successfully mixing and matching Obj-C and Swift in an Xcode 7 project. However, I can't seem to figure out how, in an Objective C class, to inherit from a Swift class (and yes I know about declaring that Swift class as @objc for visibility). In this case the desired Swift superclass MySwiftViewController is a subclass of UIViewController. For now, in Obj-C, I'm inheriting directly from UIViewController and not gaining access to the capabilities I added in MySwiftViewController.

以下是我的理解:

- 将Obj-C类声明为继承自一些东西,必须在':'之后的.h文件中:

-- To declare an Obj-C class as inheriting from something, that must be in the .h file after the ':':

#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@end

- 要使Swift类可见,那就是 #import ed:

-- To make Swift classes visible, that is #imported:

#import "MyProject-Swift.h"

但是,您无法将Swift自动生成的桥接头导入Obj-C .h 文件。您也无法使用 @class 向前声明一个不透明的超类。那么,这是可能的吗?

However, you cannot import the Swift auto-generated bridging header into the Obj-C .h file. You also cannot forward-declare an opaque superclass with @class. So, is this possible and how?

推荐答案

不幸的是,不可能在Objective-C中继承Swift类。直接来自文档:

Unfortunately, it's not possible to subclass a Swift class in Objective-C. Straight from the docs:


你不能在Objective-C中继承Swift类。

You cannot subclass a Swift class in Objective-C.

请参阅Apple的互操作性指南,了解有关使用Objective-C可以访问和不能访问的内容的更多详细信息。

See Apple's guide on interoperability for more details on what you can and cannot access with Objective-C.

这篇关于从Objective C中的Swift类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆