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

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.

Related

Xcode 8 with mixed Swift and Objective-c project generated "ModuleName-Swift.h" header not found

I have a project with mixed Swift and Objective-C in Xcode 8 that uses the generated "ModuleName-Swift.h" header file to import swift into Objective-c classes, but the preprocessor is not able to find the generated header file and throws an error on the import.
"Lexical or Preprocessor issue : 'ModuleName-Swift.h file not found'"
The project compiles just fine, but the preprocessor throws errors for the header not being found and for any Swift classes called inside the class there is no syntax highlighting or code completion. It's a struggle working with Swift classes in Objective-c that are unrecognized by Xcode 8, but yet compile just fine.
Any ideas on how to appease the preprocessor in Xcode 8?
I had exactly the same issue. Found the solution after adding a new file aiming to only one target (by mistake) and noticing that it had no problem reading the Swift classes. So, if you have multiple targets, and since the migration you didn't have the need to build and run them, suggest you to do that.
Have this problem when we have multiple targets. If the Objective-c Generated Interface Header Name variable in Swift compiler of newly created targets is different than the original target's value. Change it to the same value with original target. See the following:
change, newtargetname-Swift.h to originaltargetname-Swift.h for new target
To fix this issue Xcode 9.2, After long research i came to known my "Objective-c Generated Interface Header Name" is named after Product Name "productname-Swift.h"
You need to add this in your build settings.
There might be an issue while migrating to Xcode 8, where you will have unspecified in the build setting Swift header file.
This if from killerz
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"
If
your product's normal targets run fine, but
you get the error when running your test targets…
note that each target uses (and should use) a different filename for the Objective-C Generated Interface Header Name.
This means you cannot import the generated header file in your Objective-C .h files, because they won't be found in the test target:
Instead, you should move these #import statements into your Objective-C .m (implementation files), where they'll build successfully.
If you need to refer to Swift classes in a .h file, use the #class directive, e.g.:
//
// ViewController.h
// import-example
//
#import <UIKit/UIKit.h>
#class SomeSwiftClass;
#interface ViewController : UIViewController
- (NSString *) titleFromGenerator:(SomeSwiftClass *)generator;
#end

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?

Swift header stop compiled

I have very strange error.
I am using a Swift class in Objective-c. I have Bridging-Header.h created, import MyProject-Swift.h in my file, and all WORKS perfectly!
So, now i meet a very strange error. I made no changes in projects settings, no change in swift files or something else. When i build now the project, i receive this error MyProject-Swift.h file not found suddenly.
I checked all Settings in Project:
Defines Modules is set to YES
Objective-c generated interface header is set to MyProject-Swift.h
MyProject name is not contains space!
And isn't work for any file: .h or .m
EDIT:
Is weird because i can se swift class in my objective-c file, and i can use it, but i take always the only error: MyProject-Swift.h file not found

Expose Swift Files to Objective-C within Framework

I have a framework with Swift and Objective-C in it. I have the statement at the top of my Objective-C class #import "MyFrameworkHeader.h" which I thought would expose my swift code to my Objective-C class however the compiler still says the symbols don't exist, how can I expose my Swift classes to my Objective-C classes within the same Framework?
Ugh, after smacking my head for a few hours then finally posting this question, within a few minutes I found the answer:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_82
Under the header: "Importing Swift into Objective-C"
To get Objective-C to register your swift code you should go into your build settings and then see/set your project name, also see if the compatibility header option is checked to yes.
after that is set just go into your file and type #import "-Swift.h". I don't think you can import individual files with swift and Objective-C, you import all of your swift files at once. It's important that you make sure all of your swift files have no errors or you will get an error about this header not being defined. You might have to build your project with no errors for the file to get generated at least once.
I think what you're looking for is a Bridging Header.
This is a file that you import your Objective-C headers into that get exposed to Swift.
There are 2 ways to make one (as far as I know):
The easiest way, in my opinion, is to add an existing Obj-C file to your swift project, or vice-versa. Xcode should ask you if you want to automatically configure a bridging header. Choose yes, and Xcode should make a file called something like 'project name'-bridging-header.h. In this file, import your files, so #import "MyFrameworkHeader.h" should do it.
Make your own empty file with File > New File (Cmd+N) > Source > Header File. Call it whatever, and import your files like in the previous one. Before it will work, you have to enter the name of your bridging header file into a field in your Project Settings (In the first option, Xcode will do it for you).

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".
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:

Resources