if you don’t take #define into account, there are two notorious ways for declaring constants in objective-c project:
// Util.h
extern NSString * const MyConstant;
// Util.m
NSString * const MyConstant = @"value";
or the other directly in header file
// Util.h
static NSString *const MyConstant = @"value";
Now, there comes two questions:
1)
Both works, the second method is quite convenient, as I have only one place for editing values.
However as I saw from Apple .h files, the first method is always preferred, and I wonder if there are any downside, with the static method.
2) Looking at Apple docs, we often encounter very long constant name like: NSTextInputContextKeyboardSelectionDidChangeNotification. In the case you used a long constant name like that, what convention would you generally use to assign a value. If I want to use something descriptive I could use @”nsTextInputContextKeyboardSelectionDidChangeNotification”, but sounds little odd.
I won’t explain you all the specifics of the two types of constant declaration – in short:
The first one separates constant declaration from constant definition. Only declaration is included by header files. It also conveniently hides the actual value of the constant.
The second one is more problematic – the header file contains both declaration and definition of the constant. That means that whenewer you include the header, the constant is created again. I don’t think this will work correctly when included from multiple files.
The second question – no problem with long constant names. Your example is a bit extreme but there is nothing wrong with it.
EDIT: Adding more information about the
static NSString* constin header.Let’s have a header
A.h:and file including it
A.mFirst note that headers are removed by the preprocessor before compilation. That means that before compilation there won’t be any
A.hfile andA.mwill look like this:Let’s create another constant header
B.hand implementation fileB.mwith exactly the same contents:Note that the constant is declared twice, with the same name and different values. This is possible because
staticmakes the constant private for the file that includes it. If you remove thestatic, you’ll get a compilation error because the compiler will find two public global constants with the same name.In theory it is possible to use
static NSString* constin header files and everything will work correctly but as you can see, it doesn’t do exactly what you want and can be a source of difficult-to-find bugs. That’s why you should usestaticonly from implementation files.