Re: Keys of the kingdom
Re: Keys of the kingdom
- Subject: Re: Keys of the kingdom
- From: Shawn Erickson <email@hidden>
- Date: Sat, 22 Apr 2006 16:05:08 -0700
On Apr 22, 2006, at 3:49 PM, Boyd Collier wrote:
I had the same question as Kevin, but my c is a bit rusty. Ondra,
could you be
more specific re using extern const in this context?
In what context exactly?
If you have code like this...
[dictionary setObject:object forKey:@"My Key"];
...
[dictionary objectForKey:@"My Key"];
...it makes sense to centrally define the string constant so you only
have to track and modify it in one location.
You can do that by using macros...
#define MY_KEY @"My Key"
...but a better way IMHO is to use string constants (also what Apple
usually does). To do that you would...
---- SomeHeaderFile.h ----
extern NSString* const kMyKey;
---- SomeCodeFile.m (or .mm) ----
NSString* const kMyKey = @"My Key";
---- Some other code ----
[dictionary setObject:object forKey: kMyKey];
...
[dictionary objectForKey: kMyKey];
What "NSString* const" says is that the "kMyKey" variable is an
immutable (constant) pointer to an NSString instance and NSString is
immutable by definition and also happens to be a constant string
which cannot be deallocated.
-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden