Not able to add Objective C Bridging Header in Swift project - objective-c

I am using Xcode 8.2.1 and Swift 3. I am trying to add Objective C Bridging Header. Though I have created the .h file there is no row in the Build Settings to add the Bridging Header
I am stuck on this for hours. Any help in the right direction would be greatly appreciated.

It takes really stupidity to make this mistake. So basically I created .h file before creating a .m file. Create a dummy .m file, and Xcode asks if you want to create an Objective C Header. Create .m first before creating .h

It seems like you have the Basic option selected (at the top right of you screenshot), select the All option and the setting you need will appear.
It will be easier to see if you search for bridging instead.

Related

How do I add category to Objective C class in a pod?

I need to extend the functionality of a library Pod written in Objective C (the rest of the project is in Swift). I created the MyClass+Category .h and .m files (as a Category), with the pod selected as target, but I get a file not found error in the header at:
#import <MyLib/MyLib.h>
How do I get the category's header to find the original class?
An alternative would be to write the extension in Swift, are there any gotchas there I should be aware of?
Also grateful for any better ways there might be.
Adding a Bridging Header in Your Project. then import your category in BridgingHeader class. you easily access your category.
how to add header file please see this link.
https://stackoverflow.com/a/39615171/8687925

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"

Using Obj-C class extension in Swift?

I added some Obj-C code (the excellent Expressions) to my Swift project using Xcode's Add files... but it did not ask me if I wanted to make a bridging header. So I made one myself in the Obj-C code's group, edited it to #import the single header I needed, and made sure that file was referenced in the Swift Compiler in Build Settings. I then looked through the Obj-C code and made sure the .m files were in the target - they were, and they're listed in Compile Sources.
The header in question contains this:
#interface NSNumber (Expression)
+ (NSNumber *)numberByParsingExpression:(NSString *)expression;
+ (NSNumber *)numberByParsingExpression:(NSString *)expression withVariables:(NSDictionary *)varDictionary;
#end
Now I am trying to call this code using the same basic syntax as this post:
let result = NSNumber.numberByParsingExpression(f.1)
along with several variations on the theme. But it won't compile, "Type 'NSNumber' has no member 'numberByParsingExpression'".
Did I miss a step here?
According to https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
You can create a bridging header yourself by choosing File > New >
File > (iOS, watchOS, tvOS, or OS X) > Source > Header File.
You’ll need to edit the bridging header file to expose your
Objective-C code to your Swift code.
In your Objective-C bridging header file, import every Objective-C
header you want to expose to Swift. For example:
In Build Settings, in Swift Compiler - Code Generation, make sure the
Objective-C Bridging Header build setting under has a path to the
bridging header file. The path should be relative to your project,
similar to the way your Info.plist path is specified in Build
Settings. In most cases, you should not need to modify this setting.
Any public Objective-C headers listed in this bridging header file
will be visible to Swift. The Objective-C functionality will be
available in any Swift file within that target automatically, without
any import statements. Use your custom Objective-C code with the same
Swift syntax you use with system classes.
If you already did this correctly, and it still isn't working, try deleting the projects derived data, and clean building your project.
Ok, this turns out to be an Xcode peccadillo.
When you create the header file within the group, it actually places it physically in the source folder. So in my case the header was created in /project/subproject/.h although it appeared within Xcode to be part of the base folder, /project/.h.
So in fact there were two headers, one in the right place with nothing in it, and another in the wrong place that was the one that was being edited within Xcode. So you have to look at the file inspector to make sure it placed the bridging header in the right place!

Where is the reference to the .m file?

I created a new Xcode project (iOS application, Tabbed Application).
Now I'm seeing one sample code that Xcode generated for me (that I did not touch at all) and of course, it works on the iPhone simulator.
I am going through the code and though I'm seeing references to the .h files, I do not see any reference in any of the created files to .m files. (as in
#import "MRTAppDelegate.h"
#import "MRTFirstViewController.h"
#import "MRTSecondViewController.h"
Does the compiler just process whatever .m files you add to the project? Is there a list where they are all accounted for?
p.s. As it is obvious I also have little Objective C background, whatever I could carry from my university C classes.
if you go to build phrases -> compiled sources all the .m as specified there.
You're right on the basic assumption that Xcode will link the header and implementation for you. Just make sure you're added the .m file to your build sources and Xcode will do the rest.
You're also always going to #import the header file, and not the implementation file, in other classes.