项目范围配置变量的存储位置?

Ber*_*rnd 1 objective-c ios

可能重复:
在iOS应用程序中存储全局常量的位置?

我想将配置变量存储在一个中心位置.变量在运行时不会更改.它们应该可以从项目中的任何类访问.

我目前的方法是使用一个GlobalSettings.h文件,其配置变量定义如下

#define kCacheAge 10.0 //Max. Cache Age in Seconds
#define kNavigationTitleTint colorWithRed:0.443 green:0.471 blue:0.502 alpha:1.0
#define kNavigationTitleFont boldSystemFontOfSize:20.0
Run Code Online (Sandbox Code Playgroud)

......并在我需要的地方加入他们的名字.

缺点:

  • 我需要手动在每个类中包含此GlobalSettings.h文件
  • 我不能将这些常量作为字符串的一部分插入(字符串处理似乎比替换操作更强)

有没有更好的方法来做到这一点,也许避免上述缺点?存储配置变量的最佳实践?

ipm*_*mcc 5

如果GlobalSetting.h在项目前缀标题中包含标题,它将自动包含在项目的每个文件中.在项目中查找名为的文件<YourProjectName>-Prefix.pch.iOS项目的标准版本如下所示:

//
// Prefix header for all source files of the 'IOSTest' target in the 'IOSTest' project
//

#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>
#endif
Run Code Online (Sandbox Code Playgroud)

#import "GlobalSettings.h"在这种情况下,您将在Foundation导入下面添加.