Cannot import Objective-C code into Swift file - objective-c

I would like to import an objective c code from TwitterAuthHelper.h file into my swift code.
After that i added the header file to my project, i created the Bridging Header file that i defined " #import "TwitterAuthHelper.h" " inside of it.
The Bridging file that i added ' #import "TwitterAuthHelper.h" '
However, when i wanted to use TwitterAuthHelper(firebaseRef: .... inside that swift file, it says "use of unresolved identifier 'TwitterAuthHelper'", even though i defined the import of header file inside of the Bridging Header file.
The Swift file that i would like to import the objective C code into
Any help would be greatly appreciated.

I have had some experience with this you should make sure that you named your bringing-header file correctly. You also my clean your build and then build it again.

i'm fix this
insert #import <Accounts/Accounts.h>
in file TwitterAuthHelper.h and clean the project.

Related

Adding objective c class that uses swift classes to bridging header Projectname_swift.h not found

I have an objective-c class that uses swift classes. It all works fine.
I wanted to import the objective-c class into a swift class, so I added its header file to the bridging header. All the sudden I got an error the Projectname_swift.h file is not found.
Any ideas how to resolve this issue?
Is it actually possible?
a circular reference has been created, making it so the Swift code is unable to compile (which leads to the canary error stating that the _Swift.h file is not found).
i have provided a more in depth answer to a similar questions here and here.
long story short, the documentation explicitly says not to this:
To avoid cyclical references, don’t import Swift code into an Objective-C header (.h) file. Instead, you can forward declare a Swift class or protocol to reference it in an Objective-C interface.
Forward declarations of Swift classes and protocols can only be used as types for method and property declarations.
in order to make your code compile again you will need to remove the #import "Projectname_Swift.h" line from the offending Objective-C header. ideally you can simply move the import statement into your .m file, however if you need to publicly expose the Swift class in your ObjC header, then you must forward declare it using #class SomeSwiftClass;.
Let the Xcode build the bridge file from Objective-C to Swift.
Create a temporary directory elsewhere. In there, you create a dummy Xcode Swift project, give the project name the same as your existing Current Project Name.
Then add new file, Objective-C (.m file). The XCode will prompt you to create a bridge header file, click on the create bridge file (the right most button).
Now you locate the header file location in Finder. Then drag into your Current Project of Interest, don't forget to checked the copy file if necessary option. Add necessary #import '.....' in the header file.
You should be good. If everything works fine, delete the dummy project.
Clean derived data. and then #import "ProjectName-Swift.h" in your objective c files.
Go to
Build Settings->Objective-C Generated Interface Header Name
and set the value to YourModule-Swift.h (this is usually already set, this is the filename you need to import on .m file #import "YourModule-Swift.h"
Go to Build Settings and search for "Defines Module", set both values to YES
Create a class in swift with prefix of #objc for example
#objc class mySwiftClass{...}
Build the project again
it will be better if you use error syntax or screen shot. you can simply try this
1. Goto your project on top of right navigation
2. select build settings from middle pain.
3. search for Objective-C bridging header
4. just below this you will find "Generated interface HeaderName"
5. add correct address of your swift file
6. clean and build the project.

bridging header does not exist

I am trying to use AKPickerView pod written in Objective C in a Swift, but I keep getting this error
Bridging header '/Users/bogdanbarbulescu/Desktop/Inapk/Inapk/Pods-Inapk-umbrella.h' does not exist .
I have done the following settings:
1. Under Build Settings
Defines Module YES
Under Swift Compiler
Code Generation
Objective-C Bridging Header - set to path-
/Users/bogdanbarbulescu/Desktop/Inapk/Pods/Target Support Files/Pods- Inapk/Pods-Inapk-umbrella.h
In Pods-Inapk-umbrella.h I input this statement: #import "AKPickerView.h" [Image showing error][1]
[1]: http://i.stack.imgur.com/qcEY7.png
Please let me know if you know how can I fix this. Thanks
I'd suggest you do the following.
Instead of putting something inside the pods, create a bridging header in your project group.
Easiest way I found, is to simply create a new cocoaTouch class right clicking on your project and selecting new file.
and select objective-c for the language.
This will ask you for your permission to create a bridging header file.
In that, import the module you want to use #import "AKPickerView.h"

Add custom header to Xcode project to hold related imports in one place

I've realised that that I have like 4 or 5 related .h files that I have to import at few places. It seems reasonable to create a one .h file, add all imports to it and then only import one new file instead of 5.
First of all, is this a common practice? Or is there a nicer way?
In Xcode, when I try to do so using File -> New -> File... -> Source -> Header File the newly created header holds this:
#ifndef MyProject_MyProjetCommonHeader_h
#define MyProject_MyProjetCommonHeader_h
#endif
Not sure why these preprocessor commands needed. And it doesn't recognise (autocomplete) any headers when I try to use #import. Am I doing it wrong?
Yes you could import all the header files into MyProjectCommonHeader.h file.
#import "Header1.h"
#import "Header2.h"
And then to use Header1.h and Header2.h in another file just import MyProjectCommonHeader.h in the desired files.
The #ifndef commands are explained nicely here http://www.cprogramming.com/reference/preprocessor/ifndef.html

import modulname-Swift.h is working in .m but not in .h files

I try to mix swift and Obj-c in my project.
I made a couple of Swift classes (and protocolls).
If I put #import "ModuleName-Swift.h" to an .m file, it's working properly, but if i try to put it to a .h file, it says "ModuleName-Swift.h" file not found.
What could be the problem?
import <ModuleName-Swift.h>
try using angle brackets instead.

Xcode not recognizing CGFloat / UIFont / CGSize while using Objective-C with Swift

I created a new Swift project and tried to import my existing Objective C code into the Swift project. Everything is going fine except, Xcode 6 Beta 5 is complaining about CGFloat UIFont CGSize...The error I see is
Expect a type
and
Unknown type name 'CGFloat'
right next to some of my methods. I thought Swift should be friendly with Objective C and accept all my Objective C code but unfortunately, this is not the case.
Any Idea?, suggestions, or comments, I would appreciate it. Thanks.
just Add this in which you are facing error
#import <UIKit/UIKit.h>
Swift
import CoreGraphics
or
import UIKit
If you'd like. (UIKit includes CoreGraphics)
If you tried to import existing Objective-C code than first of all you create one Objective-C header file in your Swift project. . .
after clicking that, editor asked for creating Bridging-Header, you must click yes:
these creates yourSwiftProjectName-Bridging-Header.h file . . .
Now, import your existing objective-c file in your project & simply import your header file in Bridging-Header.h file for example,
if your objective-c file is viewController.h & viewController.m than in Bridging-Header.h file write one line code:
#import "viewController.h"
I think, this code works for you . . .
In multi module envirompment it can be because of wrong setting .modulemap[About] or umbrella header does not include it