在 bridging-header 中导入一个文件,该文件导入 Swift 标头 [英] Import a file in bridging-header which imports Swift header

查看:63
本文介绍了在 bridging-header 中导入一个文件,该文件导入 Swift 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直遇到错误,告诉我 Xcode 无法导入桥接头.我注意到只有当我导入一个特定的类时才会发生这种情况,该类导入了我的 Swift 头文件 (ProjectName-swift.h).

I keep running into an error telling me that Xcode failed to import bridging header. I notice this only happens when I import one specific class which imports my Swift header (ProjectName-swift.h).

是否可以导入一个导入 Swift 标头的类?如果没有,我如何使用在我的 Swift 类中导入 Swift 标头的类,同时仍然能够使用我在文件中需要的 Swift 代码?

Is it possible to import a class which imports the Swift header? If not, how do I use the class that imports the Swift header in my Swift classes while still being able to use the Swift code I need in the file?

推荐答案

将 Swift 头文件从类的头文件导入到该类的实现文件中,并使用 @class 进行转发Swift 类的声明.

Move the Swift header import from your class's header file to the implementation file for that class, and use @class to make a forward declaration of your Swift class.

例如,如果您需要为名为 MySwiftClass 的 Swift 类导入 Swift 头文件,则在头文件中的 @interface 之前,添加以下行:

For example, if you need to import the Swift header for a Swift class named MySwiftClass, then before your @interface in your header file, put the following line:

@class MySwiftClass;

这将允许您的头文件在不直接导入 Swift 头文件的情况下进行编译.您的实现仍然需要它,因此应该将导入语句移到实现文件中.

That will allow your header to compile without importing the Swift header file directly. Your implementation will still need it, so that's why the import statement should be moved to the implementation file.

如果您要转发的声明是协议,也可以这样做.您只需使用 @protocol 关键字.

The same can be done if what you're trying to forward declare is a protocol. You just use the @protocol keyword.

使用类或协议的前向声明是打破循环依赖的 Objective-C 方式,即使只使用 Objective-C 代码也是如此.有时甚至有必要为在同一个头文件中定义的类或协议使用前向声明,例如,如果您有一个相互引用的协议和类.

Using forward declarations of classes or protocols is the Objective-C way to break circular dependencies even when only Objective-C code is being used. Sometimes it's even necessary to use a forward declaration for a class or protocol defined in the same header file if for example you have a protocol and class that both refer to each other.

这篇关于在 bridging-header 中导入一个文件,该文件导入 Swift 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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