简体   繁体   English

迅速而客观的桥梁

[英]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: 即:

  • I have project name called: "ProjectBlaBla" 我的项目名称为:“ ProjectBlaBla”
  • I have header file called "readingPage.h" 我有一个名为“ readingPage.h”的头文件。
  • I have swift file called: "readingSwift.swift" 我有一个名为“ readingSwift.swift”的swift文件。
  • swift bridge file's name: "ProjectBlaBla-Swift.h" swift桥文件的名称:“ ProjectBlaBla-Swift.h”
  • I created header bridge file: "ProjectBlaBla-Bridging-Header.h" 我创建了头桥文件:“ ProjectBlaBla-Bridging-Header.h”
  • I imported "ProjectBlaBla-Swift.h" in "readingPage.h" file without problem and used swift classes inside objective-c 我没有问题地在“ readingPage.h”文件中导入了“ ProjectBlaBla-Swift.h”,并在objective-c中使用了swift类
  • 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类,则您应该:

  1. Remove any references to -Swift.h from readingPage.h . readingPage.h删除对-Swift.h所有引用。
  2. Import -Swift.h in readingPage.m 进口-Swift.hreadingPage.m
  3. Insert @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知道该类存在并在其他地方声明。

Check whether the bridging header path is correct. 检查桥接头路径是否正确。 On the left, select your project name -> TARGETS -> Build Settings -> search for Objective-C Bridging Header. 在左侧,选择项目名称->目标->构建设置->搜索Objective-C桥接标题。 Refer the photo below. 请参阅下面的照片。

桥接头

Two options 两种选择

  1. Import the "ProjectBlaBla-Swift.h" inside the "readingPage.m" file instead of "readingPage.h" file 在“ readingPage.m”文件而不是“ readingPage.h”文件中导入“ ProjectBlaBla-Swift.h”
  2. Create a new PCH file named "Prefix.pch" and import "ProjectBlaBla-Swift.h" inside the "Prefix.pch" file. 创建一个名为“ Prefix.pch”的新PCH文件,然后在“ Prefix.pch”文件中导入“ ProjectBlaBla-Swift.h”。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM