#import with defined prefix (possibly the product name) - ios

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.

Related

Conditional import in Objective-C

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.

Do different things on different Targets [duplicate]

I have a project I am splitting into two targets. The original single-target project uses a number of define commands, however I need these values to now be different depending on which target I am building.
What's the correct way to do that? Should I be using NStrings declared on the AppDelegate? Or can I use a #if statement when settings the #defines?
Any help, much appreciated.
One approach would be like this:
#if defined(MON_TARGET_A)
#define MON_TARGET_NAME "App A"
#elif defined(MON_TARGET_B)
#define MON_TARGET_NAME "App B"
#else
#error "which target are you building?"
#endif
Then add MON_TARGET_A or MON_TARGET_B to your target's preprocessor settings.
Usually, you'll use GCC_PREPROCESSOR_DEFINITIONS_NOT_USED_IN_PRECOMPS and not GCC_PREPROCESSOR_DEFINITIONS because the latter can prevent sharing of PCH headers.
To add this, go to:
Project Navigator -> Project -> Target -> Build Settings
then drop GCC_PREPROCESSOR_DEFINITIONS_NOT_USED_IN_PRECOMPS into the search field and set its value to something like: MON_TARGET_A $(inherited)
You can add additional preprocessor macros in your target settings (Preprocessing->Preprocessor Macros) and use #ifdef.
This is the most flexible approach.

How to access header file declaration without #include

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.

Examine filetype in prefix.pch

I am thinking about that it would be a great idea for examining that the class type. In example I would like to do the following in my application prefix.pch file.
#if isViewController
#import "DeviceCompatibility.h"
#import "UIViewController+Utilities.h"
#endif
How could I do this.
Thanks for the ideas and your time
This won't work. The point of a pch file is that it is a "Pre Compiled Header" file. It's compiled once and then that is used in every other file.
What you are asking for would require that it not be pre-compiled since it would need to be evaluated for each file. What you want is what regular, non-pch files are for.

How to specify #define commands for my two different targets

I have a project I am splitting into two targets. The original single-target project uses a number of define commands, however I need these values to now be different depending on which target I am building.
What's the correct way to do that? Should I be using NStrings declared on the AppDelegate? Or can I use a #if statement when settings the #defines?
Any help, much appreciated.
One approach would be like this:
#if defined(MON_TARGET_A)
#define MON_TARGET_NAME "App A"
#elif defined(MON_TARGET_B)
#define MON_TARGET_NAME "App B"
#else
#error "which target are you building?"
#endif
Then add MON_TARGET_A or MON_TARGET_B to your target's preprocessor settings.
Usually, you'll use GCC_PREPROCESSOR_DEFINITIONS_NOT_USED_IN_PRECOMPS and not GCC_PREPROCESSOR_DEFINITIONS because the latter can prevent sharing of PCH headers.
To add this, go to:
Project Navigator -> Project -> Target -> Build Settings
then drop GCC_PREPROCESSOR_DEFINITIONS_NOT_USED_IN_PRECOMPS into the search field and set its value to something like: MON_TARGET_A $(inherited)
You can add additional preprocessor macros in your target settings (Preprocessing->Preprocessor Macros) and use #ifdef.
This is the most flexible approach.

Resources