Swift to Objective-C header does not contain Swift classes

前端 未结 8 2060
無奈伤痛
無奈伤痛 2020-12-16 12:18

I\'m attempting to use Swift classes in my Objective-C code, however my Swift classes don\'t seem to appear in the generated header. As a result, my build fails with \"Use o

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 13:00

    tl;dr Ensure you have a bridging header if you're doing any cross-calling between Objective-C and Swift.

    I had the exact same problem: I could see the -Swift.h file in DerivedData but it made no mention of my Swift classes. I was importing the header file correctly, the Defines Module setting was YES, and the Product Module Name was correct. I tried deleting and re-adding the Swift files, clean buiild, quitting XCode, etc, with no luck.

    Then I realised I had no -Bridging-Header.h file in my project, presumably due to the way I'd cobbled it together from a previous project. Shouldn't be a problem because I was not (yet) calling Objective-C from Swift. But when I added a bridging header, and referred to its path in the build settings (Swift Compiler - Code Generation -> Objective-C Bridging Header), it magically fixed the problem - my -Swift.h file was suddenly full of SWIFT_CLASS() goodness!

    So I'm guessing the bridging header is fundamental to the process, even if you're NOT using Objective-C from Swift.


    UPDATE: I finally understand this. It is related to public/internal access modifiers. Not sure if I missed this originally or if it's an addition to the Apple docs, but it now clearly states:-

    By default, the generated header contains interfaces for Swift declarations marked with the public modifier. It also contains those marked with the internal modifier if your app target has an Objective-C bridging header.

提交回复
热议问题