My app is in swift and has some files from other modules in objective-c. App has 2 targets, lets say Target-1 and Target-2. When I build the app, a file called as "Target(number)-Swift.h" is generated in build folder.
I need to import this file into the objective-c class. Since there are 2 targets and the file name depends on target name, I need to do conditional import based on which target I am running. I want something like:
if running Target1
#import "Target1-Swift.h"
endIf
if running Target2
#import "Target2-Swift.h"
endIf
If I directly add #import "Target1-Swift.h" it works fine when I build Target1, but fails when I build Target2. I tried the following:
Code I tried:
#ifdef TARGET1_SWIFT_H
#import "Target1-Swift.h"
#endif
With this I don't see errors on imports, but the I see errors like "Use of undeclared identifier 'class_name'" for the classes declared in "-Swift.h", so looks like "-Swift.h" file it not being imported.
Any idea how can I fix this.
You can use Preprocessor macros for this.
Assign a flag for only one target. And use that flag for a condition to determine which target you're running on.
You can use it in the swift file as follow.
Related
I have an iOS project-ProjectX (not created by me) which is able to access declaration from a .h file without using #include "someHeader.h".
In ProjectX, I could just create an empty File and refer to a declaration in "someHeader.h", which I find perplexing. Example:
#import <Foundation/Foundation.h>
#implementation Empty:NSObject
SOME_TYPE_FROM_SOME_HEADER_H x;
#end
and the compiler automatically knows where the definition is?!
I have since tried to create an identical project, duplicating all the project settings, adding static libraries/files, etc. but to no avail.
Any ideas on what I might have missed out or what do I need to configure in the project to achieve this?
As mentioned by Rishab, I was missing a precompiled header (.pch) file. In the project, a pch imported a static library which contained the header file. Therefore, I was able to call the definitions directly.
I have an old .a library called BULib with a macro NEW_API defined in the GCC_PREPROCESSOR_DEFINITIONS of the library target. This macro is used inside the lib to expose some methods in the headers or not with conditional compiling. For example:
#ifdef NEW_API
+ (nullable NSString *)user;
#endif
The problem appears when using this library inside a project. When I want to use
[BULib user];
I get a compile time error
No known class method for selector 'getUser'
The problem is solved if I add NEW_API in the GCC_PREPROCESSOR_DEFINITIONS of the project, then I can use the symbol and it works as expected.
Maybe I'm misunderstanding the use of the macros between libraries and consumer project but I don't want that behavior.
Why do I have to set the macro in the consumer project if the lib has been compiled correctly with the macro set? If this is not the way to achieve this, what is it?
Thank you so much.
There are several possible solutions:
Add NEW_API in GCC_PREPROCESSOR_DEFINITIONS as you did
Delete #ifdef NEW_API and #end
Use #define NEW_API before importing libs header
Add + (nullable NSString *)user; and other missing methods in BULib's category
Personally I prefer 3rd option.
It will be something like that:
#define NEW_API
#import <bulib.h>
I wrote my own macros to output detail message in development environment
#ifdef DEBUG
#define GCLog(fmt, ...) NSLog((fmt), ##__VA_ARGS__);
#else
#define GCLog(...);
I don't want to import this in every file, and I know the shortcoming of PCH file.
So what can I do with this?
You've got four options:
Import some file containing that macro in every file you want to use
it in
Put it in the PCH which is automatically imported for you
Put it in a file and import that file in the PCH
Include that macro in every file you want to use it (generally a bad plan)
Personally when I was writing Objective-C, I would go with option 2 when I wanted something available in every file.
I have a preprocessor macro in my XCODE project called LINPHONE_DEBUG. In my build settings, I have set the macro to 0 exactly like this: LINPHONE_DEBUG=0.
But somehow when I use this code:
#if LINPHONE_DEBUG
foo()
#endif
The foo() is still called see the screenshot below.
You can see that I printed po LINPHONE_DEBUG and it is 0, but it still reached the code.
I am running XCODE 6.4 (6E35b).
From the Swift iBook: Using Swift with Cocoa and Objective-C (Swift 2 Prerease)
“The Swift compiler does not include a preprocessor. Instead, it takes advantage of compile-time attributes, build configurations, and language features to accomplish the same functionality. For this reason, preprocessor directives are not imported in Swift.”
“Build Configurations
Swift code and Objective-C code are conditionally compiled in different ways. Swift code can be conditionally compiled based on the evaluation of build configurations. Build configurations include the literal true and false values, command line flags, and the platform-testing functions listed in the table below. You can specify command line flags using -D <#flag#>.”
See the documentation for more information on Build Configurations.
Update: This solution working in objective c.
You should try like this:
#if LINPHONE_DEBUG > 0
foo()
#endif
You can avoid the preprocessor directives by putting your environment configurations into structs.
Can I import a file with a pre-defined prefix.
e.g. something like this (pseudo code)
#import PRODUCT_NAME+"-Config.h"
I want to have different targets in Xcode for my application. Each target can have its own configuration file(s). I want this to be automatic however, so if the PRODUCT_NAME bit can be the actual target product name that would be the best way.
Thanks for any help you can give me.
Many thanks.
Setup each target with a unique flag under the Other C Flags build setting. Then you can do something like:
#if defined(TARGET1)
#import "Target1Config.h"
#elif defined(TARGET2)
#import "Target2Config.h"
#endif
where TARGET1 and TARGET2 are each defined only for the corresponding target's build settings.
Just give the config files the exact same name, but locate them in different folders. Make sure that each config file only has the appropriate target ticked. Then you can just do #import "Config.h", and the relevant Config.h will be used each time.