0

I am having the project(P1) and a sub-project(P2). P2 contains a Swift file (F2.swift) and P1 contains an Objective C file (F1.h and F1.m). I was trying to access F2 in F1. How can i do that?

I tried to #import <P2-Swift.h> in F1.m but it is throwing error 'P2-Swift.h' file not found

Edit: P.S. The sub-project is static library.

Soumya
  • 1
  • 4
  • Does this answer your question? [How to access subprojects header file in main project](https://stackoverflow.com/questions/11063733/how-to-access-subprojects-header-file-in-main-project) – Jimmy James Oct 26 '20 at 08:39
  • @Jimmy I tried adding the `PATH` But it did not work for me. – Soumya Oct 26 '20 at 13:12
  • How do you integrate your sub-project into your project e.g. static or dynamic library? – iUrii Oct 26 '20 at 13:51
  • It's a static project. I made a project and Drag and Drop to another project. @iUrii – Soumya Oct 26 '20 at 13:59
  • This is resolved. https://paul-samuels.com/blog/2018/01/14/swift-static-library-in-objective-c/ I need to add modulemap and run script as mentioned in this blog. – Soumya Oct 27 '20 at 14:18

2 Answers2

0

XCode generates the header file for your static swift library next to your app's data in DerivedData folder:

/Users/user/Library/Developer/Xcode/DerivedData/
    YourApp-gwvcbdfthcschccmeafalxjghrzo
        Build
            Intermediates.noindex
                StaticLib.build
                    Debug-iphonesimulator
                        StaticLib.build
                            DerivedSources

Where 'StaticLib' is your library name.

So you should just set this path in your Build Settings > Header Search Paths:

$(OBJROOT)/StaticLib.build/$(CONFIGURATION)-$(PLATFORM_NAME)/StaticLib.build/DerivedSources

enter image description here

If you get Could not find or use auto-linked library errors later it means that your project is pure objc and you need to expand it for swift. To fix this just add empty swift file to your objc project and needed swift libraries will be included to your app.

iUrii
  • 11,742
  • 1
  • 33
  • 48
  • This is already done in my project. I am even able to access P2's Objective-c file in P1. The only problem is I am not able to access the Swift file in P1's Objective-c file. @iUrii – Soumya Oct 27 '20 at 14:17
  • @Soumya What do you mean not able to access the swift file? 'StaticLib-Swift.h' has all public classes/structs from your static library so you just need to import this header file into your .m file. – iUrii Oct 27 '20 at 15:36
0

You can add the product module name in your F1.m file and import in it like this #import "ObjCBridgingHeader-Swift.h"

This "ObjCBridgingHeader"is my Project's name and you can check this in the Product Module name.

Dharman
  • 30,962
  • 25
  • 85
  • 135