Re: Creation of constants
Re: Creation of constants
- Subject: Re: Creation of constants
- From: "Alastair J.Houghton" <email@hidden>
- Date: Fri, 31 Oct 2003 10:45:28 +0000
On Friday, October 31, 2003, at 09:35 am, j o a r wrote:
According to Apple you should avoid using the #define preprocessor
command to create constants:
<file:///Developer/Documentation/Cocoa/Conceptual/CodingGuidelines/
Articles/NamingIvarsAndTypes.html>
Why is that? And if that is true, then how would you create string
constants to use for example as dictionary keys - they're only
mentioning integer and floating point constants.
Well, one reason is that #define'd constants don't end-up as symbols in
the debugger on most systems. For integer constants, you can use
enums; you don't have to give a name... it is perfectly legal to write
e.g.
enum {
MY_CONSTANT = 5
};
For string constants, you can declare an external global variable e.g.
extern const char *MY_C_STRING;
extern NSString *MY_NS_STRING;
Then create a .m or .c file containing the actual strings
const char *MY_C_STRING = "This is a C string";
NSString *MY_NS_STRING = @"This is an NSString";
This has the advantage that the program only contains a single copy of
the strings in question, without requiring special features in the
linker to merge similar constant strings. Also, again, the symbol is
available from the debugger, which can be very handy.
If you take a look at Apple's headers, they use these techniques in
quite a few places.
Kind regards,
Alastair.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.