Where to store global constants in an iOS application?
I'd like to store config variables in a central place. The variables do not change on runtime. They should be accessible from any class in the project.
My current approach is to have a GlobalSettings.h file with the config variables defined like this
#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
... and include their name wherever I need it.
- I need to include this GlobalSettings.h file in every Class manually
- I cannot insert these constants as a part of Strings (the string handling seems to be stronger than the replacement action)
Is there a better way to do this, maybe avoiding the mentioned downsides? Any best-practices for storing config variables?
If you include your
GlobalSetting.h header in the projects Prefix Header, it will automatically be included in every file in your project. Look for a file in your project called something like
<YourProjectName>-Prefix.pch. The standard one for an iOS project looks like this:
// // 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
You would add
#import "GlobalSettings.h" below the Foundation import in this case.