Re: basic Obj-C lesson (was runtime error with NSArray no more!)
Re: basic Obj-C lesson (was runtime error with NSArray no more!)
- Subject: Re: basic Obj-C lesson (was runtime error with NSArray no more!)
- From: Mike McCune <email@hidden>
- Date: Fri, 6 Dec 2002 15:15:51 -0500
ok I guess its time for a code example, cause I'm definately retaining
the NSArray object.
here's my swap method, its meant to be called by check boxes from the
UI, just so you know statArray is the NSArray object with a collection
of NSTextField *'s and NSButton *'s in it.
- (IBAction)swap:(id)sender
{
static int swaplevel = 0;
int stat1, stat1i, stat2, stat2i;
int i;
swaplevel++;
if(swaplevel == 2)
{
swaplevel = 0;
[[statArray objectAtIndex: (stat1i * 3) + 2] setState:
NSOffState];
stat1i = 0;
for(i = 0; i < NUM_OF_STATS; i++)
{
if([[statArray objectAtIndex: (i * 3) + 2] state] ==
NSOnState)
{
stat1i = i;
[[statArray objectAtIndex: (i * 3) + 2] setState:
NSOffState];
stat1 = [[statArray objectAtIndex: (i * 3)] intValue];
}
}
stat2i = 0;
for(i = 0; i < NUM_OF_STATS; i++)
{
if([[statArray objectAtIndex: (i * 3) + 2] state] ==
NSOnState)
{
stat2i = i;
[[statArray objectAtIndex: (i * 3) + 2] setState:
NSOffState];
stat2 = [[statArray objectAtIndex: (i * 3)] intValue];
}
}
[self setStat:stat1 atIndex:stat2i];
[self setStat:stat2 atIndex:stat1i];
}
}
hope that's clear enough, and I hope even more that I haven't made some
glaring error.
thanks,
Mike McCune
OPB Studios
email@hidden
On Friday, December 6, 2002, at 02:28 PM, Sherm Pendley wrote:
<snip>
Yes - but that reference to the NSButton object will only exist for as
long as the NSArray container that holds it exists. In short, you don't
need to retain objects added to a collection - the collection does that
for you. You do, however, need to establish ownership of the collection
object itself, if you intend to use it over the lifetime of several
events. You can establish ownership either with an explicit "retain"
message to an existing object, or by using an init: or copy: method
that creates an object that has been implicitly retained on your behalf.
>
>
sherm--
>
>
If you listen to a UNIX shell, can you hear the C?
_______________________________________________
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.