Tuesday, June 29, 2010

NSString constants in Objective C

Easiest way:
// constants.h
#define MY_CONSTANT @"my_constant"
 
Better way:
// constants.h
extern NSString * const MY_CONSTANT;
// constants.m
NSString * const MY_CONSTANT = @"const value";

There is also one thing to mention. If you need a non global constant, you should use static keyword.
Example

// in constants.m file
static NSString * const MY_CONSTANT = @"const value";

The following are some interesting article.

Java Developer’s Guide to String Constants in Objective-C

Correct way of defining constants in Objective-C

No comments:

Post a Comment