Re: Initializing an Array
Re: Initializing an Array
- Subject: Re: Initializing an Array
- From: Jeff Galyan <email@hidden>
- Date: Tue, 22 Jan 2002 02:58:28 -0700
The problem is that gcc doesn't allow assignments or initialization in
headers (at least, not in ObjC mode).
Do the initialization of the array in the implementation section, and it
should be okay.
Also, this code:
>
>> NSArray *myArray = [[NSArray arrayWithObjects:@"Do
>
>> unto",@"others", nil] retain];
Be careful where you put your retain calls. I've got some code I'm debugging
right now where I did exactly like the above, but it turns out the message
is getting sent to a different instance than I expected, or rather, the
return type indicated in the docs apparently has no meaning to the runtime.
For example, one would think that because retain returns type id that that
means you can keep that pointer around. Wrong. The runtime is deallocating
all of those pointers I set up like above almost as soon as they're returned
from the receiver (stepping through in the debugger, I saw one of them last
for two statements before it evaporated... "0x0" is never an address one
likes to see appear in the vars pane). So, you have to do this:
NSArray *myArray = [NSArray arrayWithObjects:@"Do unto", @"others", nil];
[myArray retain];
Makes me wonder why retain returns anything at all.
I'm also dealing with a problem of my ivars that I send retain messages to
in my awakeFromNib methods getting deallocated after a couple method
invocations.
Point here is, ObjC's garbage collection is *way* more aggressive than
Java's, and even when you're being careful, you can get stomped on by the
runtime.
--Jeff
On 1/21/02 9:25 PM, "Erik M. Buck" <email@hidden> wrote:
>
The nil is not in the array. Cocoa collections can not store nil. The
>
NSNull class was created so that something like a nil could be stored in
>
collections.
>
>
----- Original Message -----
>
From: "Andy" <email@hidden>
>
To: "cocoa-dev" <email@hidden>
>
Sent: Monday, January 21, 2002 9:46 PM
>
Subject: Re: Initializing an Array
>
>
>
> "John C. Randolph" wrote:
>
>>
>
>> On Monday, January 21, 2002, at 02:18 PM, Michael P. Rogers wrote:
>
>>
>
>>> Is it possible to initialize an instance variable array in
>
>>> Objective-C? PB doesn't like it when I attempt to do so in the
>
>>> interface section, i.e., code like this fails:
>
>>>
>
>>> NSString * sayings[2] = {@"Do unto",@"others"};
>
>>
>
>> Try
>
>>
>
>> NSArray *myArray = [[NSArray arrayWithObjects:@"Do
>
>> unto",@"others", nil] retain];
>
>>
>
>> in your +initialize method instead.
>
>>
>
>
>
> I found this last night, and its certainly a good replacement for the
>
> similar Java construct. I just wish it were better documented... does
>
> this 'nil' end up in the NSArray after construction, ie is the length of
>
> the above 2 or 3?
>
>
>
> (I'm a few hours away from being able to call the code that I'm writing
>
> which uses this, and not knowing is gnawing at me).
>
>
>
>
>
> --
>
> AndyT (lordpixel - the cat who walks through walls)
>
> A little bigger on the inside
>
>
>
> I think we finally found the killer app for Flash: animated stick men
>
> _______________________________________________
>
> cocoa-dev mailing list | email@hidden
>
> Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
> Do not post admin requests to the list. They will be ignored.
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.