Skip to main content

Exposing all your Swift files to Objective-C in one line of code using a Precompiled Header File

Comments

6 comments

  • Justin Lin

    Have you ever run into an issue, where it works if you import the header individually into the .m files, but when you put it into the .pch file, it produces a "file not found" error?

    Here is a thread about this issue that seems to not have a solution:

    https://stackoverflow.com/questions/39471019/imported-xcode-generated-swift-header-not-found-in-pch-file

    0
  • Alex Petuschak

    Hey Justin,

     

    I. You should normally wrap the statement to import the Swift Bridging Header file under `#ifdef __OBJC__` ... `#endif`, exactly as suggested in this article.

     

    II. In my experience, using a Swift Bridging Header is usually a "two-edged sword" case:

    1) In order to have the Swift Bridging Header file generated, you need your project to compile successfully (and have at least one Objective-C and at least one Swift file);

    2) Sometimes it's tricky to get the project compiled before the Swift Bridging Header file is generated.

     

    For troubleshooting:

    i. First, try to get to the point when you can build the project successfully.

    ii. Find the Swift Bridging Header (`ProjectName-Swift.h`) under the DerivedData folder (go to Xcode -> Preferences -> Locations to navigate to this folder).

    iii. Once the above works, try importing your Swift Bridging Header file from the PCH file.

     

    Let me know how the above suggestions work for you!

    0
  • Justin Lin

    Unfortunately, it still has "ProjectModuleName-swift file not found". 

    I guess I'll just keep importing it per file. I wonder why Apple made all this integration stuff so messy. I guess pure swift projects are the future haha. 

    0
  • Alex Petuschak

    I got you, Justin.

    Indeed, that would be difficult to investigate without touching the project.

    If you think that would help, you could strip down the project that demonstrates the issue and send it privately to alex(at)swiftify.com.

    Otherwise, feel free to proceed with your approach.

    0
  • Alex Petuschak

    I have added a sample project where Objective-C <-> Swift interoperability works both ways (using the .pch file) under

    https://github.com/Swiftify-Corp/Swiftify/tree/master/BridgingHeader/TestObjc

    Hope that will be helpful to understand how things should work, and compare to your project.

    0
  • JingHui Cheng
    Hi, I have two questions
    1. Can this approach apply to the framework?
    2. The swift file imports the objective-c header file through the "-bridge-header.h" file, and the objective-c header file imports "projectName - swift.h" through the pch file, so there will be circular imports, so there should be problems ?
    
    
    Please help to answer
    0

Please sign in to leave a comment.

Powered by Zendesk