iOS - 'Myproject-Swift.H' File Not Found When Running Unit Tests for Swift

XXX-Swift.h file not found in test project

Go to Xcode Project > Build Settings and type "header name", you will get the below option:

Sample Image

Make sure that there must exist a file with a name: $(SWIFT_MODULE_NAME)-Swift.h

Then type, "bridging header" under Build Settings and make sure that the path of "bridging-header.h" file exist correctly.

Sample Image

Edited:

1) Make sure that your "Project Name" doesn't contains spaces
2) Well "XXX-Swift.h" file is generated at the following path, so you need to add that path to unit test - Go to build settings, type "Header Search Paths" and add the following line

"$(TARGET_TEMP_DIR)/../$(PROJECT_NAME).build/DerivedSources"

For more, you can refer this link : iOS - 'MyProject-Swift.h' file not found when running Unit Tests for Swift

myProjectName-Swift.h not found after clean build

You must not use #import "ProjectName-Swift.h" in the header files.

If you need Swift classes or protocols in the Obj-C code, you may forward declare them in the related Obj-C header. Here's more information about that:

When declarations in an Objective-C header file refer to a Swift class or protocol that comes from the same target, importing the generated header creates a cyclical reference. To avoid this, use a forward declaration of the Swift class or protocol to reference it in an Objective-C interface.

// MyObjcClass.h
@class MySwiftClass;
@protocol MySwiftProtocol;

@interface MyObjcClass : NSObject
- (MySwiftClass *)returnSwiftClassInstance;
- (id <MySwiftProtocol>)returnInstanceAdoptingSwiftProtocol;
// ...
@end

Also, please note, you may have issues with importing Swift Enums and Protocols and Classes into ObjC, so you may need to explicitly define items which you want to be available to ObjC code with @objc keyword.

And you won't be able to use Swift structs in Obj-C.

Importing Project-Swift.h into a Objective-C class...file not found

I was running into the same issue and couldn't get my project to import swift into obj-c classes. Using Xcode 6, (should work for Xcode 6+) and was able to do it in this way....

  1. Any class that you need to access in the .h file needs to be a forward declaration like this

@class MySwiftClass;


  1. In the .m file ONLY, if the code is in the same project (module) then you need to import it with

#import "ProductModuleName-Swift.h"

Link to the apple documentation about it

https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c

ModuleName-Swift.h file not found in xcode8

The assumption I made in Edit 4 turned out to be the correct one.
If a project has multiple targets the "-Swift.h" files cannot be imported all in one .m ObjectiveC file.

So there is one solution that must be adopted and it is to change the SWIFT_OBJC_INTERFACE_HEADER_NAME build setting and making it the same across different targets.
To do so change the instruction that generates this property from $(SWIFT_MODULE_NAME)-Swift.h to $(PROJECT_NAME)-Swift.h as explained here

It is also possible to set the Product Module Name setting in Build Settings to be the same across your modules (I set it to $(PROJECT_NAME)), so that the -Swift.h file that is generated has the same name across all modules. This eliminates the need for adding/checking preprocessor macros.

After doing this Clean Build Folder by pressing Alt and going into Product menu. Since name of header is shared among targets now it can be imported once in the .m ObjectiveC file and all targets can benefit from Swift classes.

If after building it still shows the error, ensure that the header can be reached from XCode by Cmd clicking on its name. It should open a file that contains code similar to this:

SWIFT_CLASS("_TtC27ProjectName_Summary11MyClass")
@interface MyClass : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

If need to ensure that those headers are being generated open a terminal and use this command

find ~/Library/Developer/Xcode/DerivedData -name "*Swift.h"

You should see one header for each target

Another issue that happened to me after those changes is that it started giving errors on ObjectiveC code that I didn't touch. The problem was due to the position of the import, as reported here:

Exactly where at the top of a .m file you #import the hidden bridging header can make a difference. The usual sign of trouble is that you get an “Unknown type name” compile error, where the unknown type is a class declared in Objective-C. The solution is to #import the .h file containing the declaration for the unknown type in your Objective-C files as well, before you #import the hidden bridging header. Having to do this can be an annoyance, especially if the Objective-C file in question has no need to know about this class, but it resolves the issue and allows compilation to proceed.

At the very end the code compiles and runs on device and simulator!



Related Topics



Leave a reply



Submit