Class Constants

c-preprocessor, iphone, objective-c, xcode

Solution

Have your own constant file like MyConstants.

In MyConstants.h declare all your constants:

static const int kRedisplayTileCount = 5 ;
extern  NSString* const kCellTitleKey;

and in MyConstants.m define them

NSString* const kCellTitleKey = @"CellTitle";

By keeping constants in separate file, it will be easy for you to trace them and change their value.

This is the best way to define purely constants. And this will avoid duplicate keys also.

Only you need to do is import this class in you other classes:

#import "MyConstants.h"

and use these keys straight away:

obj = [[NSUserDefaults standardUserDefaults] integerForKey: kCellTitleKey];

Problem

I have several obj-c classes, each of which require a number of constants that are used in switch statements. I had tried defining these numerical constants in the .m file using the `#define` preprocessor instruction. All these constants begin with 'kCell'. This seems to work well but Xcode's code sense is presenting me with every 'kCell' prefixed constant no matter where I am in the project. Do `#define` constants have global scope? If so, what is the best way to define purely local class constants that won't class with similarly named constants in other classes?

Original source

Related problems