ios Swift 桥接头文件不适用于 use_frameworks
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37268368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift bridging header file won't work with use_frameworks
提问by AXSM
I'm trying to use GoogleidentityToolkit library to handle login an things. I enable use_frameworks! on my pod file, but the module GITkit can't be found. I'm trying to figure out what's 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 库需要什么?
This question was asked one week after the release of CocoaPods 1.0.0 (at a time where CocoaPods 0.39.0 was still popular), and available version of Google Identity Toolkitwas 1.1.3 from 2015, but got deprecated in favor of Firebase Authentication (pod 'FirebaseUI/Auth'
) following Google I/O 2016.
这个问题是在 CocoaPods 1.0.0 发布一周后提出的(当时 CocoaPods 0.39.0 仍然很流行),Google Identity Toolkit 的可用版本是 2015 年的 1.1.3,但被弃用,以支持 Firebase 身份验证( pod 'FirebaseUI/Auth'
) 在 Google I/O 2016 之后。
回答by Pavan Gandhi
A)Create a Bridging Header file named "ProjectName-Bridging-Header.h"in the root folder of your project.
A)在项目的根文件夹中创建一个名为“ProjectName-Bridging-Header.h”的桥接头文件 。
B)Go to the project build settings and set the following values:
B)转到项目构建设置并设置以下值:
- "Install objective-c compatibility header": YES
- "Objective-C Bridging Header": path of your bridging header(e.g. "ProjectName/ProjectName-Bridging-Header.h"
- “安装objective-c 兼容性头文件”:是
- “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.
之后,您可以使用头文件导入您想要在 swift 代码中使用的所有 ObjectiveC 文件。
NOTE: if required set the path as a recursive both in the resource headers and the Swift compiler search section.
注意:如果需要,在资源头和 Swift 编译器搜索部分将路径设置为递归。
回答by Bartosz Kunat
None of the answers above worked for me or weren't precise enough. In Xcode 11.4(Swift 5.2) this solution worked for me:
上面的答案都不适合我或不够精确。在Xcode 11.4( Swift 5.2) 中,此解决方案对我有用:
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".
1.在你的项目根目录下新建一个头文件。我不确定文件名是否真的重要,但 Apple 自动生成的桥接头文件名为“ ProjectName-Bridging-Header.h”。
2.Add all the imports you need to the newly created file.
2.将您需要的所有导入添加到新创建的文件中。
3.In Project Navigator click on your project's name.
3.在项目导航器中单击您的项目名称。
4.In the topmost bar choose "Build settings", and in the one a bit lower choose Alland Combined.
4.在最上面的栏中选择“ Build settings”,在下面的一个中选择All和Combined。
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”右侧。
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!
7.您现在需要做的就是将您创建的桥接头文件拖到弹出窗口中并按回车键。你都准备好了!
*Remember that you'll have to update the path to your Bridging Header every time you project's direct path changes
*请记住,每次项目的直接路径更改时,您都必须更新桥接头的路径
回答by Mapsy
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.
我发现的最简单的方法是.swift
在 XCode 中创建一个假文件。这应该会提示自动创建桥接头。
- File> New> File...
- For the filetype, choose Swift.
- Allow Xcode to manually create the Swift Bridging Header.
- Delete the
.swift
file you originally created.
- 文件>新建>文件...
- 对于文件类型,选择Swift。
- 允许 Xcode 手动创建Swift Bridging Header。
- 删除
.swift
您最初创建的文件。
回答by MS_iOS
- Add a new file to Xcode (File > New > File), then select “Source” and click “Header File“.
- Name your file “YourProjectName-Bridging-Header.h”.
- Create the file.
- 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.
- 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.
- 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.
- 向 Xcode 添加一个新文件(文件 > 新建 > 文件),然后选择“源”并单击“头文件”。
- 将您的文件命名为“YourProjectName-Bridging-Header.h”。
- 创建文件。
- 导航到您的项目构建设置并找到“Swift Compiler – Code Generation”部分。您可能会发现在搜索框中键入“Swift Compiler”以缩小结果范围会更快。注意:如果您没有“Swift Compiler – Code Generation”部分,这意味着您可能还没有将任何 Swift 类添加到您的项目中。添加 Swift 文件,然后重试。
- 在“Objective-C Bridging Header”旁边,您需要添加头文件的名称/路径。如果您的文件位于项目的根文件夹中,只需将头文件的名称放在那里即可。示例:“ProjectName/ProjectName-Bridging-Header.h”或简称为“ProjectName-Bridging-Header.h”。或者,只需将桥接头文件从 finder 拖放到此空字段中即可。这将自动添加桥接头文件的路径。
- 打开您新创建的桥接头并使用 #import 语句导入您的 Objective-C 类。可以从您的 swift 类访问此文件中列出的任何类。
回答by Matt
Swift 4 and Xcode 9.3
Swift 4 和 Xcode 9.3
Create a Bridging Header file:
- Xcode> File/New.../File> Header File
- Name the file "ProjectName-bridging-header.h"
- Save to root of your project folder
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)
- Just below "Build Setting" make sure "All" and "Combined" is selected
- In search box type "swift compiler" and find "Objective-C Bridging Header" item
- 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)
Include the #import 's needed
创建桥接头文件:
- Xcode> 文件/新建.../文件> 头文件
- 将文件命名为“ProjectName-bridging-header.h”
- 保存到项目文件夹的根目录
Xcode> Go to Build Settings(在项目资源管理器面板中选择最上面的项目,应该是你的项目名称,在右侧面板中选择“Build Settings”主题)
- 在“构建设置”下方确保选择“全部”和“组合”
- 在搜索框中输入“swift compiler”并找到“Objective-C Bridging Header”项
- 折叠它并双击它的右侧进行编辑
- 插入上面1.的文件名->“ ProjectName/ProjectName-bridging-header.h”(如果桥接文件保存在项目文件夹中,请注意文件夹路径)
包括 #import 的需要
回答by Lotus Shah
First create briding header file with named "projectname-bridging-header.h" at your project root level.
首先在您的项目根级别创建名为“projectname-bridging-header.h”的 briding 头文件。
Now in build settings set your bridging header file path and its objc compatibility header.
现在在构建设置中设置你的桥接头文件路径和它的 objc 兼容性头文件。
Once done, Clean and build your project its work fine.
完成后,清理并构建您的项目,它的工作正常。