Re: global NSMutableDictionary
Re: global NSMutableDictionary
- Subject: Re: global NSMutableDictionary
- From: "Michael Ash" <email@hidden>
- Date: Tue, 29 Jul 2008 17:20:48 -0400
On Tue, Jul 29, 2008 at 2:04 PM, Bill Bumgarner <email@hidden> wrote:
> On Jul 29, 2008, at 10:57 AM, Nathaniel Gottlieb-Graham wrote:
>>
>> Why do externs seem to have to be NSStrings? Also, is this even the right
>> way to go about having a read/write global NSMutableDictionary? If not, how
>> would I do this?
>
> The variables cannot point to anything that isn't a constant. @"foo bar"
> is a constant string and is handled differently by the compiler/linker.
> [[NSMutableDictionary dictionary] retain] is not a constant and, thus,
> cannot be used to directly initialize a variable at time of declaration
> within a global scope.
>
> Instead, you need to initialize the variables as early as needed within your
> application.
>
> This could be done in main(). Or it could be done in the +initialize for a
> class.
>
> Or you could annotate a function as a constructor. It will run before
> main().
>
> static void __InitializeGlobalStuffMan(void) __attribute__ ((constructor));
> void __InitializeGlobalStuffMan(void) {
> myGlobalDictionary = [[NSMutableDictionary alloc] init];
> ....
> }
Is this safe to do? I would be afraid that this might execute before
the ObjC runtime is initialized, leading to pain and possibly hilarity
when the objc_msgSends execute. Of course you would know better than I
would about whether that's actually the case.
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