Re: Trying to understand -- please help...
Re: Trying to understand -- please help...
- Subject: Re: Trying to understand -- please help...
- From: Vijay Malhan <email@hidden>
- Date: Wed, 21 May 2008 21:46:20 +0530
You are not retaining the autoreleased array in your constructor. You
are over-riding the first initialization of your array(which is a
leak) while you are initializing with arrayWithObjects: call.
You can remove this line completely
cityArray = [[NSArray alloc] init];
and do a retain on this
cityArray = [[NSArray arrayWithObjects: c0, ...c9, nil] retain];
It will work :-)
Also release the array in your dealloc implementation.
Happy learning!
- Cheers
On 21-May-08, at 9:35 PM, john darnell wrote:
Hello Everyone:
I decided that it was time for a play period this morning so I set a
task for myself; to place a combo box on a dialog and
populate it with the names of ten cities--no more than that. I just
wanted to see if my understanding of the language had
progressed enough to do so.
I built a dialog in InterfaceBuilder with three objects on it:
ComboBox,
and two buttons, one set to load the cities into the
box, and one to set the default. MY header looked like this:
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject
{
IBOutlet NSComboBox *cityComboBox;
NSArray *cityArray;
}
- (IBAction) loadCities:(id)sender;
- (IBAction) setDefault:(id)sender;
@end
and my implementation looked like this:
#import "Controller.h"
@implementation Controller
- (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;
}
- (IBAction)loadCities:(id)sender
{
[cityComboBox addItemsWithObjectValues:cityArray];
}
- (IBAction)setDefault:(id)sender
{
[cityComboBox selectItemAtIndex: 6];
[cityComboBox setObjectValue:[cityComboBox
objectValueOfSelectedItem]];
}
@end
The program compiled and linked without a problem, but when I ran
it, I
got a couple of errors, both related to the fact that cityArray was
empty.
When I stepped through the code, I found that cityArray was indeed
getting filled in the init call, but by the time I got around to the
loadCities call, cityArray
was empty.
Moving the cityArray population to the loadCities call fixed the
problem, but I don't understand why cityArray was trashed when it
was a member of the object in the first place.
I am obviously trying to think of Objective C in terms of C++. Where
did my thinking go wrong?
R,
John A.M. Darnell
Team Leader
Walsworth Publishing Company
Brookfield, MO
John may also be reached at email@hidden
Trivia Question: In SciFi Channel's hit series, FARSCAPE, who played
the voice of Pilot?
_______________________________________________
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
_______________________________________________
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