How to manage constants in your application?

When you are working on iOS application that have big scope, then you should take some things into your mind, these will not only help you write better and clean code but also a lot of time will be saved, if you follow these tips.

First of all, what type of constants do we normally use in application development?

Different Control (UITextbox, UILabel, UIButton) tags
Global Enumerations
Web services URLs
Alert Message text
UIImage name like save_button.png
Dictionaries keys
User Interface Constants like font names, colors

I normally divide Constants in two categories, integer constants and string contants.

GUIConstants (integer contants)

I normally make one GUIConstants.h file and delete its GUIConstants.m file. In this file, I normally place all the global controls integer tags, positions of Label, frame sizes.
For example, in GUIConstants.h , I would write

#define NameTextFieldTag 1.0
#define IMAGEVIEW_PORT_WIDTH 304.0
#define IMAGEVIEW_IPAD_WIDTH 400.0
#define STANDARD_FONT_SIZE 17.0 //font size constants
#define TAB_BAR_TINT_COLOR [UIColor colorWithRed:0.5 green:0.0 blue:0.0 alpha:1.0]

String Constants

You can make another file like AppNameConstants and place all your string constants and enumerations over there. It is bit different than integer constants file.

In your AppNameConstants.h, you will declare your string constants for example; I will place one entry from each category that I normally use in string constants.

In header file

#import

//all webrequest urls

extern NSString * const UPDATE_PROFILE_URL_STRING;

//string module tags

extern NSString * const EmployeeRecordTag;

//Notifications

extern NSString * const FileInfoDidChangeNotification;

// resource names like images

extern NSString * const LoginImageNameBackground;

// File types

typedef enum {

DocFileType,

MovieFileType

} FileType;

Now in AppNameConstants.m you will assign values to your string constants

//string module tags

NSString * const EmployeeRecordTag = @”record”;

//Notifications

NSString * const FileInfoDidChangeNotification = @”fileInfoChanged”;

// resource names like images

NSString * const LoginImageNameBackground = @”bg.png”;

//get all files url

NSString * const UPDATE_PROFILE_URL_STRING = @”http://www.server.com/profile/users/update”;

How to use

Now for example you want to use these constants in X viewController. Import AppNameConstants.h in X viewController.m and use constants like this.

EmployeeModule.text = EmployeeRecordTag ;

If you look how we use it, we only assign name of string constant rather than hard coded string value. So each time this line accesses value of “EmployeeRecordTag” from constants file.

Using string constants like this has advantage because changing the value of constant does not cause a rebuild of entire program.

This technique is very handy because if you want to change value of any constants you will change it only in this file and it will be changed everywhere in project. It not only saves your time but also your code looks clean.

AlertMessages strings

All the alret messages or label or control titles should be placed in one resource file so that whenever you need to change you should change these in one place and rest it is changed everywhere. The best way is to use Localizable string files.

“Email” = “Please provide Email.”;

Now you should use it like this in your files

[Self showAlert:NSLocalizedString(@”AppName”, nil) message:NSLocalizedString(@”Email” , nil) ];

It will search for Email field value in resource file and get its value which is “Please provide Email.”;”. For example, in future you replaced its value in resource like this

“Email” = “Email is not correct.Please provide correct Email.”;

Now you don’t need to change this in every controller. It will be changed everywhere in your project wherever you used it.

Developer: Asif Noor (asef.noor@gmail.com)