[英]bridging swift and objective-c
I have objective-c project and I added swift files in it. 我有Objective-C项目,并在其中添加了swift文件。 i created bridge file and imported swift file in some header files without problems. 我创建了桥文件,并在某些头文件中导入了swift文件,没有任何问题。
But I need to import some header files to swift files by adding them in the "<project-name>-Bridging-Header.h"
file. 但是我需要通过将某些头文件添加到"<project-name>-Bridging-Header.h"
文件中来将它们导入到快速文件中。
If I put header file in that bridge file and this header file was import swift file before; 如果我将头文件放在该桥接文件中,并且此头文件之前是导入的swift文件; Xcode give me error message: "file not found" for the swift bridge file. Xcode给我错误消息:快速桥文件“找不到文件”。
ie: 即:
when I import "readingPage.h" in the "ProjectBlaBla-Bridging-Header.h", I got error message in "readingPage.h" said: "ProjectBlaBla-Swift.h file not found"
当我在“ ProjectBlaBla-Bridging-Header.h”中导入“ readingPage.h”时,在“ readingPage.h”中出现错误消息: "ProjectBlaBla-Swift.h file not found"
any suggestions ? 有什么建议么 ?
thanks 谢谢
You are not able to reference -Swift.h files directly or indirectly in -Bridging-Header.h files. 您无法直接或间接在-Bridging-Header.h文件中引用-Swift.h文件。
If you open -Swift.h, you will see a line near the top, in my case line 99: #import "/Users/.../...-Bridging-Header.h"
, meaning -Swift.h already imports -Bridging-Header.h, so importing back creates a circular dependency. 如果打开-Swift.h,您将在顶部附近看到一行,在我的情况下是99行: #import "/Users/.../...-Bridging-Header.h"
,意味着-Swift.h已经导入-Bridging-Header.h,因此导入回来会创建循环依赖项。
To avoid this, any header you import in -Bridging-Header.h must use forward references to Swift classes or protocols it uses as described in answers to this question . 为了避免这种情况,您在-Bridging-Header.h中导入的任何标头都必须使用对Swift类或它使用的协议的前向引用,如对此问题的回答所述 。
In short, if readingPage.h
uses a Swift class named MySwiftClass
you should: 简而言之,如果readingPage.h
使用名为MySwiftClass
的Swift类,则您应该:
-Swift.h
from readingPage.h
. 从readingPage.h
删除对-Swift.h
所有引用。 -Swift.h
in readingPage.m
进口-Swift.h
在readingPage.m
@class MySwiftClass;
插入@class MySwiftClass;
into readingPage.h
before the class is used, letting Objective-C know that such a class exists and is declared elsewhere. 在使用readingPage.h
之前将其放到readingPage.h
,让Objective-C知道该类存在并在其他地方声明。 Two options 两种选择
Note: Prefix.pch is a precompiled header which makes compiling faster. 注意:Prefix.pch是一个预编译的头文件,可加快编译速度。 You do not need to reimport any files that are imported in prefix.ch file. 您不需要重新导入在prefix.ch文件中导入的任何文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.