Bridging header file Swift - objective-c

I want to manually create a Obj-C bridging header file for my new Swift project for use with AudioKit. I am stuck. Everything seems to be correct (I even checked whether a space at the end of the file path was the problem and it isn't, and I cleared the project). As a proof I include a screenshot:
What could be wrong?

The name of the bridging header should be of the format
<target_name>-Bridging-Header.h
So if your target name is AudioMeter. Then the name of the bridging header should be
AudioMeter-Bridging-Header.h
Also make sure the path of the bridging header is specified in Xcode Build Settings under Objective-C Bridging Header option.

Related

Xcode failed to emit precompiled header?

thanks in advance for the help you will give me.
I have searched this for half a day over the internet yesterday and two hours now and I haven't found anything (more than those two links that did not help FMDatabase.h not found when using route-me library & Failed to emit precompiled header for bridging header)
So here is my problem : I just had in hands a project that a previous developer has been working on, and when I try to launch it, here I have two errors :
failed to emit precompiled header
'/Users/me/Library/Developer/Xcode/DerivedData/Alavoc-arfzuirebtfstncdztyvgvtpcbgw/Build/Intermediates.noindex/PrecompiledHeaders/Alavoc-Bridging-Header-swift_1I75NH5N40QPS-clang_30E4RG2TSVLXF.pch'
for bridging header
'/Users/me/Downloads/Alavoc-ios-master/Alavoc/bridge/Alavoc-Bridging-Header.h'
/Users/me/Downloads/Alavoc-ios-master/Alavoc/externalLib/customClass/customClassViewController.h:13:9:
error: 'FMDB/FMDB.h' file not found
There is also one fatal error wroten like this (even if I only have two errors counted, this one appears in the log above the two other ones previously described)
fatal error: module file
'/Users/me/Library/Developer/Xcode/DerivedData/ModuleCache/30E4RG2TSVLXF/Foundation-3DFYNEBRQSXST.pcm'
is out of date and needs to be rebuilt: signature mismatch note:
imported by
'/Users/me/Library/Developer/Xcode/DerivedData/Alavoc-arfzuirebtfstncdztyvgvtpcbgw/Build/Intermediates.noindex/PrecompiledHeaders/Alavoc-Bridging-Header-swift_1I75NH5N40QPS-clang_30E4RG2TSVLXF.pch'
/Users/me/Downloads/Alavoc-ios-master/Alavoc/bridge/Alavoc-Bridging-Header.h:13:9:
note: in file included from
/Users/me/Downloads/Alavoc-ios-master/Alavoc/bridge/Alavoc-Bridging-Header.h:13:
#import "customClassViewController.h"
customClassViewController.h line 13 :
#import <FMDB/FMDB.h>
I guess those errors are linked. Do you have any idea where it could come from ?
Thanks in advance for your help guys, I really appreciate it!
Edit for battlmonster (new errors) :
Here are the two errros (file not found (in Alavoc-Bridging-Header.h FMDB.h not found)) and failed to emit precompiled header :
fatal error: file
'/Users/me/Downloads/Alavoc-ios-master/Alavoc/bridge/Alavoc-Bridging-Header.h'
has been modified since the precompiled header
'/Users/me/Library/Developer/Xcode/DerivedData/Alavoc-arfzuirebtfstncdztyvgvtpcbgw/Build/Intermediates.noindex/PrecompiledHeaders/Alavoc-Bridging-Header-swift_1I75NH5N40QPS-clang_30E4RG2TSVLXF.pch'
was built note: please rebuild precompiled header
'/Users/me/Library/Developer/Xcode/DerivedData/Alavoc-arfzuirebtfstncdztyvgvtpcbgw/Build/Intermediates.noindex/PrecompiledHeaders/Alavoc-Bridging-Header-swift_1I75NH5N40QPS-clang_30E4RG2TSVLXF.pch'
/Users/me/Downloads/Alavoc-ios-master/Alavoc/bridge/Alavoc-Bridging-Header.h:29:9:
error: 'FMDB/FMDB.h' file not found
import
^ 1 error generated. <unknown>:0: error: failed to emit precompiled header
'/Users/me/Library/Developer/Xcode/DerivedData/Alavoc-arfzuirebtfstncdztyvgvtpcbgw/Build/Intermediates.noindex/PrecompiledHeaders/Alavoc-Bridging-Header-swift_1I75NH5N40QPS-clang_30E4RG2TSVLXF.pch'
for bridging header
'/Users/me/Downloads/Alavoc-ios-master/Alavoc/bridge/Alavoc-Bridging-Header.h'
This error is about a malformed bridging header. The bridging header is a special header file which lists all Objective-C header files with classes that must be accessible from Swift code. All the bridging header definitions are precompiled in a way to be ready to use from Swift. In your case the bridging header is "Alavoc/bridge/Alavoc-Bridging-Header.h", and it includes a header for customClassViewController.h (from Alavoc/externalLib/customClass), which indicates that your fellow developer wants that customClassViewController is accessible in Swift code.
Now the confusing thing about the bridging header is that it is not recursively including everything, i.e. it just looks on the first level of definitions, and if you import something in your import that you want in Swift, you have to add it to the bridging header explicitly, or else you'll probably get a warning (or an error sometimes). Say you have #import "A.h" in the bridging header, and you have #import "B.h" inside "A.h", then you likely would have to add "B.h" to the bridging header as well.
Now in your example A = customClassViewController, and B = FMDB, and normally you would need to add FMDB to the bridging header, but the thing is that you most likely don't want exporting FMDB to Swift via your bridging header, because it is not meant for this (it is for your own objc code and not for 3rd party libs).
The solution would be to remove line 13 from your "customClassViewController.h". This would likely fix the bridging header compilation, but probably break the customClassViewController, so you need to include FMDB in "customClassViewController.m" and most likely adapt the "customClassViewController.h" to not have anything related to FMDB (or forward-declare those usages with #class X;).
If you move #import <FMDB/FMDB.h> to your implementation (.m) files and still get error: 'FMDB/FMDB.h' file not found, it is likely about FMDB path not being listed in your header search paths.
To solve the last one just include the right path in your "Header Search Paths" in Xcode build settings. Let's say FMDB is located at /Users/me/Downloads/Alavoc-ios-master/Alavoc/ASDASD/FMDB (and you have /Users/me/Downloads/Alavoc-ios-master/Alavoc/ASDASD/FMDB/FMDB.h inside), Then you need to open Xcode project settings - select your target on the left - select "Build Settings" on the top - find "Header Search Paths" setting and add /Users/me/Downloads/Alavoc-ios-master/Alavoc/ASDASD path
If you are using cocoapod and it is a framework, suggesting you NOT to include this in pre-compiled header.
Instead, objc files, use:
#import framework_name;

How to add Objective-C Bridging Header entry?

I have a Swift project and have add a cocoapod, which is written in Objective-C. It has header and implementation files. From what I understand, to use/import these files into my Swift files, I need to add a bridging file.
I found this site describing how to do this manually, since the Objective-C files are already part of my project (from the cocoapod).
http://www.learnswiftonline.com/getting-started/adding-swift-bridging-header/
1.) Navigate to your project build settings and find the “Swift Compiler – Code Generation” section. You may find it faster to type in “Swift Compiler” into the search box to narrow down the results.
2.) Next to “Objective-C Bridging Header” you will need to add the name/path of your header file. If your file resides in your project’s root folder simply put the name of the header file there.
I don't have a Objective-C Bridging Header in that section and it doesn't appear you can add new entries there.
I'm using Xcode 7.3.1. Anyone have some idea how this should be done?
Are you sure you looked at the correct Build Settings section, search with the keyword Swift compiler - General in the search field as describe below and then you can find it.
You need to create the header file first. It is a regular Objective-C header file and should be named <Your app or framework name>-Bridging-Header.h. For any Objective-C headers you want Swift to know about add an import statement to the newly created header file. Then follow your previous steps.
There is also a hidden header that gets created for you called <Your app or framework name>-Swift.h. If you need to access any Swift classes from an Objective-C file import this header.

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!

File not found error in bridging header file

I have an objective-c project. It is about two years that I am working on it. Now I want to do new works with Swift.
I added a new Swift class. Xcode asked me to add BridggingHeader file and I pressed YES.
but now in the bridging header file I want to import my current objective-c classed but this file do not recongize my objective-c files.
For example when I write:
#import "MYBaseViewController.h"
xcode shows error and says:
MYBaseViewController.h file not found
Check that bridging header is set correctly for each target by going to Build Settings.
Make sure that the file is included in both targets. Select it in the Project Navigator and then open File Inspector and check target Membership section:

Creating a Bridging Header

I am creating a Bridging Header manually since it doesn't let me do it automatically. I created the a header file called "AppName-Bridging-Header.h. The problem is that I can not set it as the project’s Bridging header, which can be normally done through the project’s build settings. I do not have the Objective-C Bridging file section under Swift Compiler -Code generation. I only have Optimization level. Thank you.
Your question is a bit unclear but I'll try to help you as much as I can.
1) To create a bridging header, there are two different ways :
With the help of Xcode : When you add a file to your project, Xcode asks if you want to create a bridging header.
Manually : You have to create a header file and name it Your Project-Bridging-Header.h (This is the general syntax)
2) Link the header :
If Xcode created the bridging header it is automatically linked under : -> Build Settings -> Swift Compiler - Code Generation -> Objective-C Bridging Header
If you created manually you have to go here under the "Build Settings" tab and then search for "Bridging Header. Here then you have the row "Objective-C Bridging Header" and you just have to put the the name of the folder he's in and his name. Like that : Your Project/Your Project-Bridging-Header.h. The name of the folder is defined as the screenshot shows. Doesn't matter if the project name (first arrow) and the first box (second arrow) are different, just take the right one.
Hope this helps someone :)
Sorry for explaining from the start as it is not asked in the question but sometimes it is good to have the all way of doing