import swift class in objective-c, -Swift.h file not found

后端 未结 10 1557
野性不改
野性不改 2020-12-02 15:41

I have an iOS project written with Objective-C. I created an Swift class in the project, the bridging header file for accessing objective-c in Swift is generated successfull

相关标签:
10条回答
  • 2020-12-02 15:58

    Updated May 2018 Xcode 9.3


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

    (Example, Project named CData)

    1. Same as Step 1, Go to Build Settings and search for "Defines Module", set both values to YES

    2. Create a class that extends NSObject on .swift file

    1. Build the project again

    2. Import YourModule-Swift.h file on .m file (Please notice it's case sensitive, Mymodule !== MyModule)

    0 讨论(0)
  • 2020-12-02 16:04

    OMG.. the actual import statement was not "class-Swift.h" but rather "projectname-Swift.h"

    You can find the name of the file if you look under build settings->Swift Compiler Code Generation -> Objective-C Generated Interface Header Name

    The file was not generated when I dragged in Swift source into the GUI. Only when I right-clicked->Add file to "project". It then asked to generate the header files.

    0 讨论(0)
  • 2020-12-02 16:10

    For me the solution was to create a new target. For an unknown reason, the target that I had didn't have that "Swift Compiler - General" settings and thus no to "Objective-C Generated Interface Header Name" field. Having that field specified in the project was not enough.

    0 讨论(0)
  • 2020-12-02 16:13

    My addition to Daniel Kroms answer:

    • Never add -Swift.h Header to header. Even if it seems to work. Add the Import to .m file only!

    • In case you use in your header swift classes, make a forward declaration with @class swiftclassname before your @interface

    Then you will see your real errors in your code.

    0 讨论(0)
  • 2020-12-02 16:13

    For me, the problem was that I had bitcode on. When I clicked on the "Update to recommended project settings", it changed a few settings which probably the culprit. I turned "Enabled Bitcode" to "No" in the Build Settings and it is fixed now.

    0 讨论(0)
  • 2020-12-02 16:18

    I was stacked this for a quite a while. In my case, my target name is something like "my-app" using dash as a part of target name. I tried to #import "my-app-Swift.h", but Xcode kept giving me errors.

    I dug under 'DerivedData' folder and I found "my_app-Swift.h". So if you are using some interesting characters for the target name. You may try replace those with underscore _.

    0 讨论(0)
提交回复
热议问题