• 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: Create an NSCharacterSet constant
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Create an NSCharacterSet constant


  • Subject: Re: Create an NSCharacterSet constant
  • From: Michael Ash <email@hidden>
  • Date: Mon, 2 Nov 2009 11:20:14 -0500

On Mon, Nov 2, 2009 at 12:29 AM, Kyle Sluder <email@hidden> wrote:
> On Sun, Nov 1, 2009 at 9:19 PM, John Joyce
> <email@hidden> wrote:
>> Is it possible to create an NSCharacterSet constant?
>
> No.  By definition, constants are assigned values at compile time.
> Objects other than strings can only be created at runtime.  There is
> no way around this fact.
>
>> Is it best just to declare a global constant string, then initialize an
>> NSCharacterSet during app initialization? It certainly seems that is the
>> only real option.
>
> Answers will vary.  You could use a static global variable which is
> assigned in +initialize, or you could use a static variable in a
> category on NSCharacterSet, or a number of other things.  Basically
> what you really want is a singleton, not a constant.
>
> I'm partial to the following construct:
>
> + (Foo *)sharedFoo {
>  static Foo *sharedFoo;
>  dispatch_once_t once;
>  dispatch_once(&once, ^{ sharedFoo = [[foo alloc] init]; }
>  return sharedfoo;
> }

Watch out! The 'once' variable must be declared static as well.
Without it, if you're unlucky to get a zero-filled 'once' every time
through, this call will appear to work but leak a new Foo every time
you call it.

Mike
_______________________________________________

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

  • Follow-Ups:
    • Re: Create an NSCharacterSet constant
      • From: Kyle Sluder <email@hidden>
References: 
 >Create an NSCharacterSet constant (From: John Joyce <email@hidden>)
 >Re: Create an NSCharacterSet constant (From: Kyle Sluder <email@hidden>)

  • Prev by Date: Re: NSInvocation and methods with non-pointer parameters
  • Next by Date: Re: Create an NSCharacterSet constant
  • Previous by thread: Re: Create an NSCharacterSet constant
  • Next by thread: Re: Create an NSCharacterSet constant
  • Index(es):
    • Date
    • Thread