When you write testing code you will often want to define a constant. Typical constants that you might want to factor out are your test fixtures.
Often Objective-C programmers take the approach of defining the constant like this:
#define USER_NAME @"Tom"
This is a preprocessor directive and will be replaced by the preprocess by the corresponding string “Tom”. If this define would be declared a header file the preprocessor will blindly replace all occurrences of USER_NAME.
To solve this problem you should make use of the compiler. There is always a better way to define a constant than using a preprocessor define.
static const NSString* kUserName = @"Tom";
Note that with this style, there is type information, wich is beneficial because it clearly defines what the constant is. Also note how the constant is named. The usual convention for constants is to prefix with the letter k for constants that are local to translation unit (.m files). For constants that are exposed outside of a class, it is usual to prefix with the class name to avoid name clashing conflicts. Event the static const as it stands should not appear in header file. Since Objective-C has no namespaces, it would declare a global variable called kUserName.
Therefore define translation unit specific constants within an implementation file as static const. These constants will not be exposed to the global namespace.
Define global constants as external in a header file, and define them in the associated implementation file. These constants will appear in the global symbol table, so their names should be namespaced, usually by prefixing them with the class name to which they correspond.
// SCTestData.h
extern const NSString* SCTestDataUserName;
// SCTestData.m
const NSString* SCTestDataUserName = @”Tom”
