[翻译]  Swift bridging header file wont work

[CHINESE]  Swift桥接头文件不会工作

Im trying to use GoogleidentityToolkit library to handle login an things. I enable use_frameworks! on my pod file, but the module GITkit cant be found. I'm trying to figure out whats going. As far as I know if you use "use_frameworks" you don't need to create any bridging header file, since cocoapods compiles down the library into a single module, so later you can imported as usual on your*.swift files.

我试图使用GoogleidentityToolkit库来处理登录的事情。我启用use_frameworks!在我的pod文件上,但找不到模块GITkit。我想知道最新情况。据我所知,如果使用“use_frameworks”,则不需要创建任何桥接头文件,因为cocoapods将库编译成单个模块,因此稍后您可以像往常一样导入* .swift文件。

What do I need to get using Google Identity Toolkit library in swift?.

在swift中使用Google Identity Toolkit库需要什么?

6 个解决方案



A) Create a Bridging Header file named "ProjectName-Bridging-Header.h" in the root folder of your project.


B) Go to the project build settings and set the following values:


  • "Install objective-c compatibility header" : YES
  • “安装objective-c兼容标头”:是
  • "Objective-C Bridging Header" : path of your bridging header (e.g. "ProjectName/ProjectName-Bridging-Header.h"
  • “Objective-C Bridging Header”:桥接头的路径(例如“ProjectName / ProjectName-Bridging-Header.h”

After that you can use the header file to import all your ObjectiveC files which you want use within swift code.


NOTE: if required set the path as a recursive both in the resource headers and the Swift compiler search section.




The easiest way I've found is to create a fake .swift file within XCode. This should bring up the prompt to automatically create a bridging header.


  1. File > New > File...
  2. 文件>新建>文件...
  3. For the filetype, choose Swift.
  4. 对于filetype,选择Swift。
  5. Allow Xcode to manually create the Swift Bridging Header.
  6. 允许Xcode手动创建Swift Bridging Header。
  7. Delete the .swift file you originally created.
  8. 删除最初创建的.swift文件。



  1. Add a new file to Xcode (File > New > File), then select “Source” and click “Header File“.
  2. 将新文件添加到Xcode(文件>新建>文件),然后选择“源”并单击“标题文件”。
  3. Name your file “YourProjectName-Bridging-Header.h”.
  4. 将文件命名为“YourProjectName-Bridging-Header.h”。
  5. Create the file.
  6. 创建文件。
  7. Navigate to your project build settings and find the “Swift Compiler – Code Generation” section. You may find it faster to type in “Swift Compiler” into the search box to narrow down the results. Note: If you don’t have a “Swift Compiler – Code Generation” section, this means you probably don’t have any Swift classes added to your project yet. Add a Swift file, then try again.
  8. 导航到项目构建设置并找到“Swift编译器 - 代码生成”部分。您可能会发现在搜索框中键入“Swift Compiler”以缩小结果范围会更快。注意:如果您没有“Swift编译器 - 代码生成”部分,这意味着您可能还没有向项目中添加任何Swift类。添加Swift文件,然后重试。
  9. Next to “Objective-C Bridging Header” you will need to add the name/path of your header file. If your file resides in your project’s root folder simply put the name of the header file there. Examples: “ProjectName/ProjectName-Bridging-Header.h” or simply “ProjectName-Bridging-Header.h”.Or, simply drag and drop bridging header file from finder to this empty field. This will automatically add the path of bridging header file.
  10. 在“Objective-C Bridging Header”旁边,您需要添加头文件的名称/路径。如果您的文件位于项目的根文件夹中,只需将头文件的名称放在那里。示例:“ProjectName / ProjectName-Bridging-Header.h”或简称为“ProjectName-Bridging-Header.h”。或者,只需将查找器中的桥接头文件拖放到此空字段即可。这将自动添加桥接头文件的路径。
  11. Open up your newly created bridging header and import your Objective-C classes using #import statements. Any class listed in this file will be able to be accessed from your swift classes.
  12. 打开新创建的桥接头,并使用#import语句导入Objective-C类。可以从swift类中访问此文件中列出的任何类。



None of the answers above worked for me or weren't precise enough. In Xcode 9.4.1 (Swift 4) this solution worked for me:

上面的答案都不适合我,或者不够精确。在Xcode 9.4.1(Swift 4)中,这个解决方案对我有用:

1. Create a new header file in your project's root directory. I'm not sure if the name of the file actually matters, but Apple's auto-generated bridging header files are named "ProjectName-Bridging-Header.h".


2. Add all the imports you need to the newly created file.


3. In Project Navigator click on your project's name.

3.在Project Navigator中,单击项目的名称。

4. In the topmost bar choose "Build settings", and in the one a bit lower choose All and Combined.


enter image description here

5. Search for "Swift Compiler" in the upper right corner

5.在右上角搜索“Swift Compiler”

6. Find "Swift Compiler- General" tab, expand it and double-click the right side of "Objective-C Bridging Header".

6.找到“Swift Compiler- General”选项卡,展开它,然后双击“Objective-C Bridging Header”的右侧。

enter image description here

7. All you need to do now is just drag the bridging header file you've created into the pop-up window and hit enter. You're all set!




Swift 4 and Xcode 9.3

Swift 4和Xcode 9.3

  1. Create a Bridging Header file:


    • Xcode> File/New.../File> Header File
    • Xcode>文件/新建... /文件>标题文件
    • Name the file "ProjectName-bridging-header.h"
    • 将文件命名为“ProjectName-bridging-header.h”
    • Save to root of your project folder
    • 保存到项目文件夹的根目录
  2. Xcode> Go to Build Settings (In the project explorer pane select the top most item, should be your project name and in the right pane select the "Build Settings" topic)

    Xcode>转到Build Settings(在项目浏览器窗格中选择最顶层的项目,应该是您的项目名称,并在右侧窗格中选择“Build Settings”主题)

    • Just below "Build Setting" make sure "All" and "Combined" is selected
    • 在“Build Setting”下方,确保选中“All”和“Combined”
    • In search box type "swift compiler" and find "Objective-C Bridging Header" item
    • 在搜索框中键入“swift compiler”并找到“Objective-C Bridging Header”项
    • Collapse it and double click to the right of it to edit
    • 将其折叠并双击其右侧进行编辑
    • Insert the file name of 1. above -> "ProjectName/ProjectName-bridging-header.h" (note the folder path if bridging file is saved in project folder)
    • 在上面插入文件名1. - >“ProjectName / ProjectName-bridging-header.h”(如果桥接文件保存在项目文件夹中,请注意文件夹路径)
  3. Include the #import 's needed




First create briding header file with named "projectname-bridging-header.h" at your project root level.


Now in build settings set your bridging header file path and its objc compatibility header.


Once done, Clean and build your project its work fine.




© 2014-2018 ITdaan.com 粤ICP备14056181号