Re: Defining and using constants in Objective C
Re: Defining and using constants in Objective C
- Subject: Re: Defining and using constants in Objective C
- From: Ken Thomases <email@hidden>
- Date: Mon, 30 Nov 2009 10:18:23 -0600
On Nov 30, 2009, at 12:41 AM, Kenny Leung wrote:
--------- Constants.h ---------------
extern const NSString *Suction;
--------- Constants.m ---------------
const NSString *Suction = @"Ball";
That should be:
extern NSString* const Suction;
and:
NSString* const Suction = @"Ball";
The difference is pointer-to-const-NSString vs. const-pointer-to-
NSString. You want the latter for two reason. First, you don't want
somebody to be able to reassign Suction. As you declared/defined it,
they could.
Second, the NSString's internal contents _may_ in fact not be
constant, even though the string is immutable. (For example, it
could, in theory, cache an alternative encoding of its contents if
it's asked for it, to make subsequent requests faster. I admit this
is unlikely.) In any case, you'll get compiler warnings if you try to
assign or pass a pointer-to-const-NSString where a pointer-to-NSString
is expected.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden