简体   繁体   中英

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. (Create a .h file, name it <#PROJECT_NAME>-Bridging-Header.h, import all the .h files you need?)

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.

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.

  2. Set "Objective-C Bridging Header" to <#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.

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.

I clicked yes, and it worked!

1) create a file called "FMDB-Bridging-Header.h"

inside this file type the following: #import "FMDB.h"

3) go to Build Settings -> Swift Compiler - Code Generation - add to 'Objective-C Bridging Header': FMDB-Bridging-Header.h

or if it was placed inside a folder in your project:

FolderName/FMDB-Bridging-Header.h

  1. Add a header file to your project with the name "[your-project-name]-Bridging-Header.h

  1. Go to Build Settings > Build Options and set "Embedded Content Contains Swift Code" to "Yes" 在此输入图像描述

  2. Go to Build Settings > Linking and add "@executable_path/Frameworks" to Runpath Search Paths 在此输入图像描述

Build your project now!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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