• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Defining and using constants in Objective C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Defining and using constants in Objective C (From: Tharindu Madushanka <email@hidden>)
 >Re: Defining and using constants in Objective C (From: Kenny Leung <email@hidden>)

  • Prev by Date: Re: NSWorkspace in a daemon
  • Next by Date: Re: Apache Module
  • Previous by thread: Re: Defining and using constants in Objective C
  • Next by thread: RGBA UIColor Information At CGPoint?
  • Index(es):
    • Date
    • Thread