简体   繁体   English

Swift“Bridging-Header.h”文件不允许我在.swift文件中实例化objective-c类

[英]Swift “Bridging-Header.h” file not allowing me to instantiate objective-c classes in .swift files

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. 当X代码尝试自动创建桥接头时,它每次都会崩溃,因此我按照有关如何手动创建桥接头的说明进行操作。 (Create a .h file, name it <#PROJECT_NAME>-Bridging-Header.h, import all the .h files you need?) (创建.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? 有谁知道如何修理它?

You need to add it to your target's build settings: 您需要将其添加到目标的构建设置:

  1. 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. 在Xcode中,如果你进入目标的构建设置,并一直向下滚动,你会发现一个“Swift编译器 - 代码生成”部分。

  2. Set "Objective-C Bridging Header" to <#PROJECT_NAME>-Bridging-Header.h 将“Objective-C Bridging Header”设置为<#PROJECT_NAME>-Bridging-Header.h

  3. 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. 我不确定“安装Objective-C兼容性标题”的正确值,但它是肯定/否定,所以如果它最初不起作用,您可以切换它。

I tried to create a bridging header myself but for some reason Xcode didn't like it. 我试图自己创建一个桥接头,但由于某种原因Xcode不喜欢它。 So i deleted my custom one, imported an Obj C file which made Xcode ask if I wanted it to create one for me. 所以我删除了我的自定义文件,导入了一个Obj C文件,该文件使Xcode询问我是否希望它为我创建一个。

I clicked yes, and it worked! 我点击了,它有效!

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 - 代码生成 - 添加到'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桥接,Header.h

  1. Add a header file to your project with the name "[your-project-name]-Bridging-Header.h 使用名称“[your-project-name] -Bridging-Header.h”将头文件添加到项目中

  1. Go to Build Settings > Build Options and set "Embedded Content Contains Swift Code" to "Yes" 转到Build Settings> Build Options并将“Embedded Content包含Swift Code”设置为“Yes” 在此输入图像描述

  2. Go to Build Settings > Linking and add "@executable_path/Frameworks" to Runpath Search Paths 转到Build Settings> Linking并将“@ executable_path / Frameworks”添加到Runpath搜索路径 在此输入图像描述

Build your project now! 立即构建您的项目!

它可以帮助用它的Project根设置桥接头的名称,将“MyProject / MyProject-Bridging-Header.h”设置为Swift编译器构建密钥'Objective-C Bridging Header'的字符串值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在swift中导入OBJ-C类时,Xcode不会提示我添加一个Bridging-header.h - When I import OBJ-C classes in swift, Xcode doesn't prompt me to add a Bridging-header.h 无法导入导入Project-Swift.h的Swift桥接头Objective-C文件 - Can't import into Swift bridging header Objective-C file that imports Project-Swift.h 如果包含Project-Swift.h的导入,如何将Objective-C文件导入桥接头 - How to import Objective-C file to bridging header if it contains an import of Project-Swift.h Objective-C桥接标头不适用于Swift文件 - Objective-C Bridging Header Not Working for Swift File 如何找到Bridging-Header.h的路径 - Swift,Xcode - How to find path of Bridging-Header.h - Swift, Xcode 我需要在objective-c 中使用swift 类并且无法设置我的Emilos-Swift.h 桥接文件来解析objective-c 中的任何swift 类 - I need to use a swift class in objective-c and cannot setup my Emilos-Swift.h bridging file to resolve any swift classes in objective-c 迅速而客观的桥梁 - bridging swift and objective-c 使用Swift和Objective-C桥接标题和框架时发生命名空间冲突 - Namespace conflict on bridging header and framework with swift and objective-c 如何在 Swift 项目中使用 Objective-C 桥接头 - How to use Objective-C bridging header in a Swift project Swift Bridging-Header Objective-C presentViewController - Swift Bridging-Header Objective-C presentViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM