Ios Iphone在prefix.pch中编写代码

Ios Iphone在prefix.pch中编写代码,ios,iphone,objective-c,Ios,Iphone,Objective C,我想用prefix.pch为chatboost编写代码 目前,我已经创建了一个全局文件名,其中提到了所有应用程序ID和签名 当前我的.pch文件如下所示 #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 <

我想用prefix.pch为chatboost编写代码 目前,我已经创建了一个全局文件名,其中提到了所有应用程序ID和签名

当前我的.pch文件如下所示

#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
#import <Foundation/Foundation.h>

@interface Global : NSObject
#define APP_ID @"dfgdf";
#define APP_SIGNATURE @"fdgfd";
@end
#导入
#ifndef\uuu IPHONE\u5\u0
#警告“此项目使用仅在iOS SDK 5.0及更高版本中可用的功能。”
#恩迪夫
#ifdef__OBJC__
#进口
#进口
#导入“Global.h”
#导入“Chartboost.h”
#恩迪夫
但我不知道如何在prefix.pch中编写代码

我的global.h文件如下所示

#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
#import <Foundation/Foundation.h>

@interface Global : NSObject
#define APP_ID @"dfgdf";
#define APP_SIGNATURE @"fdgfd";
@end
#导入
@接口全局:NSObject
#定义应用程序ID@“dfgdf”;
#定义APP_签名@“fdgfd”;
@结束
您可以使用#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];
#导入
#ifndef\uuu IPHONE\u5\u0
#警告“此项目使用仅在iOS SDK 5.0及更高版本中可用的功能。”
#恩迪夫
#ifdef__OBJC__
#进口
#进口
#导入“Global.h”
#导入“Chartboost.h”
#恩迪夫
#定义SHOW_CHARTBOOST[[CHARTBOOST sharedChartboost]ShowInterstitual];
并在任何其他类中使用
SHOW\u CHARTBOOST


不过,这不是一个好的做法,您应该使用全局/常量类来编写此类内容,并且主要针对一些常量,而不是其中的长方法。

对应用程序级变量使用extern键,如下所示

在.h文件中 extern NSString*您的变量

在.m文件中 NSString*yourVariable=@


您可以在任何地方使用变量名访问此变量。导入此.h文件只需使用变量名即可。

预编译的头文件用于一个目的:加快编译速度。它被编译并存储在缓存中,并在编译期间自动包含在每个源文件中。就像每个源文件一样

#import "Prefix.h"
这对于项目范围的#定义非常方便。(仅供参考,#定义为代码气味)

Xcode引号:如果前缀头或其包含的任何文件的内容很少更改,那么预编译前缀头将最有效。如果前缀头或其包含的任何文件的内容经常更改,则可能会对总体生成时间产生负面影响

更清楚的解释是

在.pch中导入源文件头时,请记住这一点。我建议您探索其他编写代码的方法,而不是选择.pch文件

您可以使用Prefix.h来导入常量和实用程序源文件。此外,为了便于进行如下调试:

#ifndef DEBUG
#define NSLog(x,...)
#endif
我看到您想要声明常量字符串以在项目范围内使用。创建一个新的头文件“Constants.h”(或“Global.h”,根据您的喜好),并在此处写入所有全局常量(通常是宏和typedef enum)。然而,要使用extern声明常量字符串,还需要实现文件

在“Constants.h”中

在“Constants.m”中


希望有帮助

我建议您直接将宏写入.pch文件。不需要为定义宏创建单独的类。写如下

#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";

若要在不使用类的情况下访问该值,请按如下所示创建类并将其导入.pch文件中

全球

#import <Foundation/Foundation.h>

@interface Global : NSObject

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

@end

我建议使用第一种方法。

您打算在.pch文件中编写什么样的代码?我找不到确切的URL,但我几乎肯定苹果建议您在另一个文件中编写宏之类的代码,并将其导入.pch,而不是直接在.pch.extern NSString*const APP_ID中编码;应该这样做:)
#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
#import <Foundation/Foundation.h>

@interface Global : NSObject

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

@end
#import "Global.h"

@implementation Global

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