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

家住魔仙堡 提交于 2019-12-17 03:23:22

问题


I have a project that was started in Objective-C, and I am trying to import some Swift code into the same class files that I have previously written Objective-C in.

I have consulted the Apple docs on using Swift and Objective-C in the same project, as well as SO question like this, but still no avail: I continue to get the file not found error after putting in #import "NewTestApp-Swift.h" (NewTestApp is the name of the Product and module).

Here is what I have done so far:

  1. In Define Modules, selected YES for the app.
  2. Ensured that the Product Module name did not have any space in it (see screenshot below question)

I have tried using #import "NewTestApp-Swift.h" inside ViewController.m, ViewController.h and AppDelegate.m but none of them has worked.

What else am I doing incorrectly? Thanks for your help.


Screenshot of settings:


Errors that I am presently encountering:


回答1:


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/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_87




回答2:


If the Swift code is inside a Module (like in your case):

#import <ProductName/ProductModuleName-Swift.h>

If the Swift code is inside the project (mixed Swift and ObjC):

#import <ProductModuleName-Swift.h>

In your case, you have to add this line in the *.m file:

#import <NewTestApp/NewTestApp-Swift.h>

IMPORTANT: look at the "<" in the import statement

https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html




回答3:


How I managed to import swift into objective-c:

  • Defines Module set to YES (on project - not on target)
  • Product Module Name set (on target - not on project)
  • In your ViewController.m import the swift code with:

    #import "MyProductModuleName-Swift.h"
    
  • Add a swift file to your objective-c project (File -> New -> Swift) and Xcode will create the bridging header from objective-c to Swift but this is crucial for making it work the other way around too - apparently.

For the last piece in this puzzle thanks to Swiftoverload for making me aware of actually adding a Swift file via Xcode GUI and not just dragging and dropping existing swift-files into my project for making it work: http://nikolakirev.com/blog/using-swift-in-objective-c-project




回答4:


Using Xcode 8.2.1 and if you look at Project > Build Settings > Objective-C Generated Interface Header Name, there it shows only one header file named like Product-Swift.h

This means that instead of importing each modules separately from Objective-C .m file, using individual -Swift.h file, you just import one Product-Swift.h which aggregated all Swift modules.

I encountered the same problem by looking for traditional way of importing modules separately, but the current version of Xcode and Swift 3 changed it to use only one header for all module importing.




回答5:


I was having problems importing Swift into an Objective-C project. In the end I looked into the Derivied Data folder to check what Xcode 7 was generating. The filename was completely different to the one I was expecting.

Once I had the actual filename I imported that and the project could build.




回答6:


Spent an hour on this issue, following these steps will help you to understand what's missing:

  1. Open Xcode preference and navigate to DerivedData folder
  2. Search for "swift.h" in finder
  3. If you can not find any project-swift.h file, this file haven't been generated. You usually need to add @objc to one of your swift class and successfully build the app, only then will Xcode generate this file
  4. If you found "xxx-swift.h" file, make sure your import statement contains the correct name.




回答7:


DEFINE MODULES: YES and import "ProjectName-Swift.h" in .m file of Obj-C class

This worked for me to access Swift classes in Obj-c.




回答8:


Had faced the same problem with my team when was working on project using git. One developer hasn't updated Xcode to the last version (7.3) which was required for latest Swift 2.2 version. So, compiler hasn't recognized new Swift syntax and couldn't generate interface for Swift library (projectname-swift.h). Check if Xcode version is the latest one!




回答9:


During development, there might be a possibility that you would have any swift class that is not completely implemented due to which there might be some syntax errors. The swift header file will be available for use only if all the swift files are error free.

Make sure there are no syntax errors in the swift files and then try to import the swift header in the objective - c file




回答10:


Importing the header file, i.e.

#import "<ProjectName>-Swift.h" 

within the .h file generated an error, stating:

-Swift.h' file not found

and the build failed.

Instead use:

#import "<ProjectName>-Swift.h"

within the .m file, and even though the same error appears, running the project anyway suppresses the error.

The swift classes are then available within the .m file.




回答11:


-Swift.h should be created by Xcode automatically if Swift code expose an API via @objc or @objcMembers [About]

Usually a location looks like

~/Library/Developer/Xcode/DerivedData/
  ProductModuleName-foo/
    Build/
      Intermediates.noindex/
        ProductModuleName.build/
          Debug-iphoneos/
            ProductModuleName.build/
              DerivedSources/
                ProductModuleName-Swift.h

It can be changed by

Project editor -> select a target -> Build Settings -> Per-configuration Intermediate Build Files Path

By default the value is $(PROJECT_TEMP_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)




回答12:


I faced the problem with the name of project (target). It included symbol "-". So the decision was next: if name of project is "Test-App", then name of imported class is "Test_App-Swift.h"




回答13:


If you have multiple target make sure that you have build all frameworks



来源:https://stackoverflow.com/questions/26328034/importing-project-swift-h-into-a-objective-c-class-file-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!