1

I want to write code in prefix.pch for chatboost currently i have made a file name global where i have mentioned all app ids and signatures

currently my .pch file looks like this

#import <Availability.h>


#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "Global.h"
    #import "Chartboost.h"
#endif

but i have no idea how to write code in prefix.pch

my global.h file looks like this

#import <Foundation/Foundation.h>

@interface Global : NSObject
#define APP_ID @"dfgdf";
#define APP_SIGNATURE @"fdgfd";
@end
  • What kind of code are you planning to write in .pch file? – Ajith Renjala Mar 18 '14 at 05:59
  • I can't find the exact URL, but I'm almost positive apple suggests that you code things like macros in another file and import that .h into your .pch as opposed to coding directly in the .pch. – Logan Mar 18 '14 at 07:06

4 Answers4

2

Pre-compiled header files were brought to serve one purpose: to make compiling faster. It is compiled and stored in cache, and automatically included in every source file during the compilation time. Its like each source file does,

#import "Prefix.h"

This can be handy for project-wide #defines. (FYI, #defines are a code smell)

Xcode quotes: Precompiling the prefix header will be most effective if the contents of the prefix header or any file it includes change rarely. If the contents of the prefix header or any file it includes change frequently, there may be a negative impact to overall build time.

More clear explanation is here

Please keep this in mind when you #import s source file header in .pch. I would suggest you to explore other ways to write your code rather than choosing .pch file.

You can use Prefix.h for #import of constants and utility source files. Also for convenience in debugging like this:

#ifndef DEBUG
#define NSLog(x,...)
#endif

I see that you want to declare constant strings to use project-wide. Create a new header file "Constants.h"(or "Global.h", as you like it) and write all your global constants(usually macros & typedef enum's) here. However, to declare constant strings using extern you would need implementation file too.

In "Constants.h",

extern NSString *const app_ID;

And in "Constants.m",

NSString *const app_ID=@"dfgdf";

Hope that helps.

Community
  • 1
  • 1
Ajith Renjala
  • 4,934
  • 5
  • 34
  • 42
0

You can write code using #define like

#import <Availability.h>


#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "Global.h"
    #import "Chartboost.h"
#endif

#define SHOW_CHARTBOOST   [[Chartboost sharedChartboost] showInterstitial];

and call it in any other class using SHOW_CHARTBOOST

This is not a good practice though, you should use global/constant class to code such stuff and also mostly for some constants rather than long methods in there.

Shubhank
  • 21,721
  • 8
  • 65
  • 83
0

I suggest you to directly write your Macro into .pch file. No need to create separate Class for defining Macro. Write as follows.

#import <Availability.h>


#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Chartboost.h"
#endif

#define APP_ID @"dfgdf";
#define APP_SIGNATURE @"fdgfd";

OR

If you want your id and signature as member of class, then Create a singleton class. Define it as a property in header file as readonly property and define value in sharedObject method. write as follows

Global.h

#import <Foundation/Foundation.h>

@interface Global : NSObject

@property(nonatomic, strong, readonly) NSString *appId;
@property(nonatomic, strong, readonly) NSString *appSignature;

+(instancetype)sharedInstance;

@end

Global.m

#import "Global.h"

@interface Global ()

@property(nonatomic, strong) NSString *appId;
@property(nonatomic, strong) NSString *appSignature;

@end

@implementation Global

+(instancetype)sharedInstance
{
   static Global *sharedInstance;

   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
       sharedInstance = [Global new];
       sharedInstance.appId = @"dfgdf";
       sharedInstance.appSignature = @"fdgfd";
   });

   return sharedInstance;
}

@end

OR

If you want to access that value without class, create class as follows and import it in .pch file.

global.h

#import <Foundation/Foundation.h>

@interface Global : NSObject

extern NSString *const appId;
extern NSString *const appSignature;

@end

global.m

#import "Global.h"

@implementation Global

 NSString *const appId = @"dfgdf";
 NSString *const appSignature = @"fdgfd";
@end

I recommend first method to use.

Rajath Shetty K
  • 424
  • 3
  • 9
-1

Use extern key work for application level variables as follow

In .h file extern NSString *yourVariable;

In .m file NSString *yourVariable = @"";

you can access this variable in any where using variable name only thing you need to import this .h file.

Chathurka
  • 605
  • 6
  • 11