Re: Trying to understand -- please help...
Re: Trying to understand -- please help...
- Subject: Re: Trying to understand -- please help...
- From: Michael Vannorsdel <email@hidden>
- Date: Wed, 21 May 2008 13:07:34 -0600
You troubles are all in this method and how you handle cityArray.
First you don't need to alloc and init a new array for cityArray as
[NSArray arrayWithObjects: c0, ...c9, nil] gives you a new one already
with the objects you specified.
Another thing you should watch for is there's an autorelease pool you
can't see that's removing objects every time your code returns to the
run loop (knowing when this happens takes experience and some doc
reading). But a beginner rule of thumb is once your code leaves your
controller's control (returns to a caller you didn't make), you have
to protect autoreleased objects by sending them a retain, stating that
you want to keep that object around.
As a general convention Cocoa, methods with "alloc", "new", "copy",
"create" in them return objects you are responsible for releasing.
Objects returned by everything else is considered autoreleased unless
otherwise noted in the API. See Create Rule and Get Rule in the docs
for more on this.
arrayWithObjects: is returning you a new array, but the name of the
method does not have the magic words listed above, so the array that
is returned is autoreleased, meaning it will be automatically
discarded in the near future.
So here's a revised version of your init:
- (id) init
{
NSString *c0 = @"New York"; //Ten NSString objects created here
...
NSString *c9 = @"Virginia Beach";
cityArray = [[NSArray arrayWithObjects: c0, ...c9, nil] retain]; //
we want to save this array from being discarded
return self;
}
And when you don't need cityArray anymore or are going to replace it
with a new array with new objects, you want to send the old one a
release to signal you don't need it around anymore.
On May 21, 2008, at 10:05 AM, john darnell wrote:
- (id) init
{
cityArray = [[NSArray alloc] init];
NSString *c0 = @"New York"; //Ten NSString objects created here
...
NSString *c9 = @"Virginia Beach";
cityArray = [NSArray arrayWithObjects: c0, ...c9, nil];
return self;
}
_______________________________________________
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