Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Bridging Header and visibility of Obj-C class

I've aded bridging header, specified in build settings the full path to it, bridging header was created automatically. After this, i've included my obj-c header files in it. But every attempt of calling constructor of object fails : "Use of undeclared identifier". enter image description here

The list of things i've done :

  1. Created .m file and Xcode proposed to create bridging header
  2. Added obj-c files to project and imported them in header
  3. In build setting provided the FULL path to bridging header file

enter image description here

  1. Used Obj-C type in code... But it doesn't builds.
  2. Then, i provided not the full path to the header, but the path from the folder in which project is - no result.

I double-checked all the steps according to apple documentation, but no result. Why? Any help would be appreciated.

like image 889
Nikita Semenov Avatar asked Jun 28 '14 15:06

Nikita Semenov


People also ask

How do you make a bridging header in Objective-C?

To create an Objective-C bridging header file, all you need to do is drag some Objective-C code into your Swift project – Xcode should prompt you with the message "Would you like to configure an Objective-C bridging header?" Click "Creating Bridging Header" and you'll see a file called YourProjectName-Bridging-Header.

How do you access Objective-C functions from Swift?

To use Objective-C code in Swift, it is only necessary to import Objective-C header file in bridging header file to expose it to Swift. If you are working on a framework, you will need to import Objective-C in umbrella header since bridging headers are not available for frameworks.

How do I import a Swift file into a bridging header?

In the Prefix Header field, add the path to the PCH file you just created $(PROJECT_DIR)/$(PROJECT_NAME)/(Project-Name)-Prefix. pch. In there, you will need to import the automatically generate Swift Bridging Header which will be named something like (Project_Name)-Swift. h.


1 Answers

Follow these steps:

  1. Create a Swift project
  2. Add a test class as Cocoa Class instead of .m and .h separately. Xcode prompt add bridging header.
  3. Import test class header in bridging header, which you already did. Should have no issue instantiate test class in Swift.
  4. Copy BL_KeyChainWrapper .m and .h to project directory in finder.
  5. Drag BL_KeyChainWrapper files to project and make sure Add to Targets.
  6. Import BL_KeyChainWrapper header in bridging header.
  7. Instantiate BL_KeyChainWrapper class in Swift.

If followed the above steps, and still have the error. It is probably that you didn't declare a class named BL_KeyChainWrapper in BL_KeyChainWrapper.h. Make sure in your BL_KeyChainWrapper.h, you have code like following:

@interface BL_KeyChainWrapper : BaseClass
like image 89
vladof81 Avatar answered Sep 29 '22 20:09

vladof81