Swift Bridging Header and visibility of Obj-C class - ios

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".
The list of things i've done :
Created .m file and Xcode proposed to create bridging header
Added obj-c files to project and imported them in header
In build setting provided the FULL path to bridging header file
Used Obj-C type in code... But it doesn't builds.
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.

Follow these steps:
Create a Swift project
Add a test class as Cocoa Class instead of .m and .h separately. Xcode prompt add bridging header.
Import test class header in bridging header, which you already did. Should have no issue instantiate test class in Swift.
Copy BL_KeyChainWrapper .m and .h to project directory in finder.
Drag BL_KeyChainWrapper files to project and make sure Add to Targets.
Import BL_KeyChainWrapper header in bridging header.
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

Well, after creating a test project with bridging header, I found out the following:
I added .m file, Xcode proposed me to create bridging header.
I added .h file, named it as class and created Obj-C class in this way.
AND:
In build settings - code generation section looks like this:
Bridging file located at the following path:
ProjName / BridgHeader.h
At the same level as .xcodeproj file exists.
BUT:
When I added already created Obj-C class to project and added header import in bridging header, I couldn't use it:
So, I guess, Xcode 6 beta 2 cannot add existing files to swift project. Did anyone face this trouble? Cause I don't want to paste all existing libraries, that I was developing for 5 years to created files.
P.S:
Bridging header:

Related

imported xcode generated swift header not found in .pch file

I have recently added a Swift file to my Objective-C only project. Xcode created the necessary header files, including the Objective-C Generated Interface Header. Everything works fine if I #import this header file "myProject-Swift.h" to the .m file where I am accessing the Swift class I have added.
However I will want to eventually use this Swift class throughout my project, and to avoid importing it every single time, I wanted to add this myProject-Swift.h import file to my .pch prefix file.
However when I try to build my app after having added this, I get a 'myProject-Swift.h' file not found error and the build fails.
Does this mean it is not possible to import this myProject-Swift.h in a .pch file?
Is it really only possible to do it in each .m file individually?

How to solve Bridiging header error in swift

Trying to call objective c code in swift code but it don't let me to do, it tell header file is missing.
The easiest way to create an Objective-C bridging header and all the project settings to support it is to add any Objective-C file to your project.
Go to File\New\File…, select iOS\Source\Cocoa Touch Class and then click Next. Enter FakeObjectiveCClass as the Class name and choose NSObject as the subclass. Also, make sure the Language is set to Objective-C! Click Next, then Create.
When prompted Would you like to configure an Objective-C bridging header? select Yes.
You’ve successfully created an Objective-C bridging header. You can delete FakeObjectiveCClass.m and FakeObjectiveCClass.h from the project now, since you really just needed the bridging header.
I got an solution for bridging-header.h file does not exit. Just want to share, hope it may help others too.
Follow below steps:-
First of all delete all header file if you have implemented any.
Add new header file from the ios templet. Hope you know how to add any new file, just follow in same manner.
Now go to the buid setting and there search for objective-c bridging header, simply put the header file name with .h extension too.
please see the image how to add the file name in objective-c Bridging Header
In Xcode, add a new class with Objective-C as the language. When prompted to create a bridging header file, click yes.
You will have a file added to your project named: YourProjectName-Bridging-Header.h.
Add all your imports to this file.
In your Swift classes, import the bridging-header file.

Mixing Swift and Objective-C : "ProjectName-Swift.h" file not found

I'm working on an iOS project that contains Swift and Objective-C Classes.
To instantiate an object described in Objective-C, I've understand that I needed to have a Bridging-Header file that contains all the imports of the headers I will have to use on my Swift classes. And it works great on my project.
I made all classes I needed, I subclassed Objective-C classes in Swift, everything is OK.
My problem is when it comes to do the opposite: instantiate a swift object in an Objective-C file. So I read that I need to have those options:
Define Modules as Yes
add the line #import "<#YourProjectName#>-Swift.h" on the *.m file I'm working on.
And that is what I did:
I changed all the values on the Build Settings that needed to be changed. I added the import line, I even tried to create the header file, but I still have the "<#YourProjectName#>-Swift.h" file not found error.
Of course, I replaced the YourProjectName by the actual module name, the one I found under Product Module Name in Packaging section on Build Settings. Also, there is no spaces in the Project Name.
Did I forgot a step?
Make sure you are importing the -Swift.h file from a .m Objective-C file and not a .h header file. The docs only talk about importing from .m:
... import the Xcode-generated header file for your Swift code into any Objective-C .m file you want to use your Swift code from.
The -Swift.h file is generated during the build and I've found that importing it from a .h file always fails, even when I know the file exists. Since it's generated by the build, I'm guessing it's cleaned out when the compiler is sifting through the headers and regenerated once the .swift files are compiled.

When I import OBJ-C classes in swift, Xcode doesn't prompt me to add a Bridging-header.h

When I first import an OBJ-C class into a swift project, Xcode doesn't prompt me to add a bridging-header file.
so I fixed in the same way as in Swift Bridging Header and visibility of Obj-C class
But when I want to import another Obj-C class, if I do the same thing again, the file's url will be mixed so that Xcode can't distinguish them.
I tried to make a bridging-header file by myself , but didn't work either.
Help please.
To make bridging header manually first create header file. Name it whatever you want. Then Click your project's target and open Build Settings tab and search "Bridging". You will see "Objective-C Bridging Header" option. Double click it and write your bridging header name like this format TargetName/BrdgingFile.h
ok,NOW I fixed this problem and in addition I figured out how "bridging header"file works.
Actually I followed the method of the question I mentioned above, but what I need to drag is exactly the "bridging-header" file, rather than other implicit header file (because you can only import one header file there.)
So I created a header file called "myproject-bridging-header.h" (actually the name doesn't matter) and dragged it to the item, and in the header file I imported other header files
Bingo! It works smoothly~

Swift Can't Import Sqlite3 iOS

I added libsqlite3.0.dylib to my project, and then I tried to import using the following code:
import UIKit
import sqlite3
class Dataware: NSObject
{
}
But it's giving me this error:
No Such Module 'sqlite3'
Add it to your Bridging-Header.h file:
#import <sqlite3.h>
This is the primary mechanism for importing any C-language libraries.
If you don't yet have a Bridging-Header.h file:
Add a file Bridging-Header.h (or more typically (ProjectName)-Bridging-Header.h
Go to the build settings tab for your project
Find "Objective-C Bridging Header". The easiest way is to search for bridging.
Enter the name and path for the file you created in step one. It's probably (ProjectName)/(ProjectName)-Bridging-Header.h
when one want to add sqlite to framework target, module.map is needed
since sqlite is not mapped, and to do so just:
1. create file in your project 'module/module.map'
2. create the module from the umbrella header:
module sqlite3 [system] {
header "/Applications/Xcode6-Beta5.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.0.sdk/usr/include/sqlite3.h"
link "sqlite3"
export *
}
*change the Xcode6-Beta5.app in the path to right one
3. connect the map file to you project, search for 'import paths' in 'Build Settings'and put the full path to the module file
We need to import the header files for SQLite3 into the view controller so that the compiler can see the function and other definitions that make up the API.
There is no way to directly import the header file into Swift code, because the SQLite3 library is not packaged as a module.
The easiest way to deal with this is to add a bridging header to the project. Once you have a bridging header, you can add other header files to it, and those header files will be read by the Swift compiler. There are a couple of ways to add a bridging file. We’ll use the simpler of the two, which is to temporarily add an Objective-C class to the project. Let’s do that now.
File ➤ New ➤ File.... In the iOS section of the dialog, choose
Cocoa Touch Class and press Next. Name the class Temporary, make it a subclass of NSObject, change the language to Objective-C, and press Next. In the next screen, press the Create button.
When you do this, Xcode will pop up a window asking whether you want to create a bridging header. Press Yes.
Now, in the Project Navigator, you’ll see the files for the new class (Temporary.m and Temporary.h) and the bridging header, which is called SQLite Persistence-Bridging-Header.h. Delete the Temporary.m and Temporary.h files—you don’t need them anymore. Select the bridging header to open it in the editor, and then add the following line to it:
#import < sqlite3.h>
Now that the compiler can see the SQLite3 library and header files, we can write some more code in ViewController.swift
That's it!
Hi Please follow these steps
In xcode 8.3.3 using swift 3
Go to Build Phases tab
Go to Link Binary with Libraries sub tab.
(a) Click + button to add sqlite framework then search for sqlite then you can see libsqlite3.0.tbd and libsqlite3.tbd
(b) Then select only libsqlite3.tbd(Don't add both because the compiler can not find sqlite3 stuct when you declare in viewController)
Then Add Bridging-Header.h file (because sqlite is not written in swift)
Bridging name should be your Projectname-Bridging-Header.h file (Just for naming convention, not mandatory)
Write #import <sqlite3.h> in your Bridging-Header file
Go to build settings tab
(a) Under the build settings tab search for Swift Compiler - General option and set YES to Install Objective-C compatibility Header
(b) Set your name and path for the header file in Objective-C Bridging Header option (Or you can simply drag the bridging header file)

Resources