Swift C header object file with multiple goals

I successfully call my Swift classes from Objective-C (for the target 'MyApp') via the import statement:

#import "MyApp-Swift.h" 

Now I have created a new target called "MyAppLite"

When I compile a new target, I get errors because the code "MyApp-Swift.h" is required by the code, but the compiler creates "MyAppLite-Swift.h"

So, I need to create a Swift / ObjC #import conditional header for the purpose for which I am compiling.

How can this be done, or is there a better way?

+44
header-files objective-c swift target
Oct 08 '14 at 16:39
source share
6 answers

You can also set the Product Module Name parameter in Build Settings same for all modules (I set it to $(PROJECT_NAME) ), so the generated <project>-Swift.h has the same name in all modules. This eliminates the need to add / check preprocessor macros.

+53
Dec 10 '14 at
source share
— -

The best way to find this problem is in the settings of the general Xcode project. You can find the following setting:

Objective-C Name of the generated interface header *

If you set this value to a common header name, such as "MyProject-Swift.h", it will be applied to each of your goals, and you can safely use the import declaration in any of your Objective-C files. This allows you to continue to use unique product module names for each purpose, if necessary.

I tested this in Xcode Version 6.4 (6E35b).

* Note. This will be displayed according to your Swift compiler settings, which are only visible if the Swift source files are added to your project. Also, if the target does not have a Swift source associated with it, the Swift compiler options will not be displayed for these target build settings.

Good luck

+39
Jul 10 '15 at 19:15
source share

The previous answers have some problems if you decide to rename your goals or project or use SWIFT_MODULE_NAME as intended.

The most universal solution is to change SWIFT_OBJC_INTERFACE_HEADER_NAME ("Objective-C Name of the generated interface header") in the "Project" section, and not "Objectives", "Assembly Parameters" to:

  • $(PROJECT_NAME)-Swift.h - one per project;
  • $(SWIFT_MODULE_NAME)-Swift.h - one per module (default value).

enter image description here

+11
Jan 09 '17 at 8:28
source share

Well, the only way I can fix it is ...

 #ifdef IS_LITE #import "MyApp_Lite-Swift.h" #else #import "MyApp-Swift.h" #endif 

Please note that if there are any “illegal” characters in my product module name, they must be replaced with underscores.

Hope this helps!

+3
Oct 13 '14 at 15:35
source share

I placed the corresponding #import <project> -Swift.h statement in the prefix header file (<project> -Prefix.pch) defined / added for each assembly (target / diagram).

+3
Aug 25 '15 at 16:23
source share

The only working way is:

1 - from the first target (which has a working bridge) Configure the assembly, select Object C Bridge header
2- Copy Object C Bridge Header
3- open another target Build setup
4- Insert it 5- change the header file to a new header file (for example, target Bh)

(you now have this option for two purposes)

0
Jan 18 '17 at 11:27
source share



All Articles