Swift Bridging Header File Won't Work with Use_Frameworks

Swift bridging header file won't work with use_frameworks

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 Bridging Header" : path of your bridging header (e.g. "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.

If using Cocoapods and use_frameworks!, should I use Obj-C Bridging Header?

Short answer: yes.

It has nothing to do with use_frameworks!, it has to do with exposing your Obj-C pods to Swift.

See examples:

  • https://stackoverflow.com/a/31742131/218152
  • https://stackoverflow.com/a/31777069/218152

Getting file not found in Bridging Header when importing Objective-C frameworks into Swift project

Found a solution:

  • The "Objective-C Bridging Header" setting (aka SWIFT_OBJC_BRIDGING_HEADER) must be set at the Target level, and NOT the Project level. Be sure to delete the setting value at the Project level.

(to me, it seems like an Xcode bug, since I don't know why it fixes it).

Bridging-header works, but imports are not working?

The answer is to import them like this in your bridging header:

#import "NBPhoneNumberUtil.h"
#import "NBPhoneNumber.h"

The procedure that I used to test this is written below.

Create a new Xcode project

I used the Single View Application template for Objective-C.

Create Podfile and Install

Podfile:

source 'https://github.com/CocoaPods/Specs.git'
pod 'libPhoneNumber-iOS', '~> 0.8'

Install:

$ pod install

Create a Swift file

At this point a bridging header file was created by Xcode.

I added the imports into the bridging header using:

#import "NBPhoneNumberUtil.h"
#import "NBPhoneNumber.h"

In the Swift file, I wrote:

class Test {
func test() {
let util = NBPhoneNumberUtil()
}
}

The project compiled without errors.



Related Topics



Leave a reply



Submit