views:

13

answers:

2

How to make a ConstantList class in Objective C of an application which could be accessible to all the classes who are using constants.

like in Actionscript we do:

public class ConstantList
{
   public static const EVENT_CHANGE:String = "event_change";
}

Or what is the best approach to handle application constant.

Regards Ranjan

+1  A: 

You can use global constants, like the following:

//MyConstants.m    
NSString * const EVENT_CHANGE = @"event_change";

// MyConstants.h
extern NSString* const EVENT_CHANGE;

Now include MyConstants.h header to your implementation file and you can use EVENT_CHANGE constant string in it

Vladimir
Thanks Vladimir.I am getting this error: "expected specifier-qualifier-list before 'extern' "
iWasRobot
could you post the full line that gives you that error?
Vladimir
This what I got.
iWasRobot
I suspect you didn't `#import <Foundation/Foundation.h>` at the top of your own header file.
Chris Hanson
A: 

I would recommend Vladimir's approach.

Just for completeness: You can do it as a class like this:

@interface Constants : NSObject {
}
+ (NSString*)aConstantString;
@end

@implementation Constants
+ (NSString*)aConstantString {
    return @"This is always the same and accessible from everywhere";
}
@end

You access the value like:

NSString* string = [Constants aConstantString];
tonklon