ios Swift“Bridging-Header.h”文件不允许我在.swift文件中实例化objective-c类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24102386/
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.h" file not allowing me to instantiate objective-c classes in .swift files
提问by Andy Tsen
When X-code tries to create a bridging header automatically, it crashes every single time, so I followed the instructions on how to manually create a bridging header. (Create a .h file, name it <#PROJECT_NAME>-Bridging-Header.h, import all the .h files you need?)
当 X-code 尝试自动创建桥接头时,它每次都会崩溃,因此我按照有关如何手动创建桥接头的说明进行操作。(创建一个 .h 文件,将其命名为 <#PROJECT_NAME>-Bridging-Header.h,导入您需要的所有 .h 文件?)
Problem is, when I try to instantiate a class in the .swift file that's included in that header, nothing happens (it says that class doesn't exist) Also, in the Bridging Header it doesn't seem to autocomplete my filenames when I try to include them, leading me to believe somethings not linking properly.
问题是,当我尝试在包含在该标头中的 .swift 文件中实例化一个类时,没有任何反应(它表示该类不存在)此外,在桥接标头中,它似乎没有自动完成我的文件名,当我尝试包括它们,让我相信某些东西没有正确链接。
Has anyone run into this? Does anyone know how to fix it?
有没有人遇到过这个?有谁知道如何修理它?
回答by Nate Cook
You need to add it to your target's build settings:
您需要将其添加到目标的构建设置中:
In Xcode, if you go into the build settings for your target, and scroll all the way down you'll find a "Swift Compiler - Code Generation" section.
Set "Objective-C Bridging Header" to
<#PROJECT_NAME>-Bridging-Header.h
I'm not sure of the correct value for "Install Objective-C Compatibility Header", but it's a yes/no, so you can toggle that if it doesn't work at first.
在 Xcode 中,如果您进入目标的构建设置,并一直向下滚动,您将找到“Swift Compiler - Code Generation”部分。
将“Objective-C 桥接头”设置为
<#PROJECT_NAME>-Bridging-Header.h
我不确定“安装 Objective-C 兼容性标头”的正确值,但它是一个是/否,因此如果一开始不起作用,您可以切换它。
回答by Sean
I tried to create a bridging header myself but for some reason Xcode didn't like it. So i deleted my custom one, imported an Obj C file which made Xcode ask if I wanted it to create one for me.
我试图自己创建一个桥接头,但由于某种原因 Xcode 不喜欢它。所以我删除了我的自定义文件,导入了一个 Obj C 文件,这让 Xcode 询问我是否希望它为我创建一个。
I clicked yes, and it worked!
我点击是,它奏效了!
回答by K.D
1) create a file called "FMDB-Bridging-Header.h"
1)创建一个名为“FMDB-Bridging-Header.h”的文件
inside this file type the following: #import "FMDB.h"
在此文件中键入以下内容:#import "FMDB.h"
3) go to Build Settings -> Swift Compiler - Code Generation - add to 'Objective-C Bridging Header': FMDB-Bridging-Header.h
3) 转到 Build Settings -> Swift Compiler - Code Generation - 添加到“Objective-C Bridging Header”:FMDB-Bridging-Header.h
or if it was placed inside a folder in your project:
或者如果它被放置在您项目的文件夹中:
FolderName/FMDB-Bridging-Header.h
文件夹名称/FMDB-Bridging-Header.h
回答by Furqan Khan
- Add a header file to your project with the name "[your-project-name]-Bridging-Header.h
- 将一个名为“[your-project-name]-Bridging-Header.h”的头文件添加到您的项目中
Go to Build Settings > Build Options and set "Embedded Content Contains Swift Code" to "Yes"
Go to Build Settings > Linking and add "@executable_path/Frameworks" to Runpath Search Paths
Build your project now!
立即构建您的项目!
回答by Nicola Coppola
it could help setting the name of the bridging header with its Project root, as "MyProject/MyProject-Bridging-Header.h" into the string value of the Swift Compiler Build key 'Objective-C Bridging Header'
它可以帮助将桥接头的名称及其项目根设置为“MyProject/MyProject-Bridging-Header.h”到 Swift Compiler Build 键“Objective-C Bridging Header”的字符串值中