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

时间:2013-01-05 13:51:59

标签: objective-c ios

  

可能重复:
  Where to store global constants in an iOS application?

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

我目前的方法是使用 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

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

缺点:

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

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

2 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:-2)

一个选项是INI文件,因为一旦值发生变化,它们就不需要重新编译。

可以找到一个简单的(我未经测试的)示例in this thread