开发者

How to make a ConstantList Class in Objective C

开发者 https://www.devze.com 2023-01-12 22:02 出处:网络
How to make a ConstantList class in Objective C of an application which could be accessible to all the classes who are using constants.

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


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


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消