Sorry, forgot to cc the group on this.
:// Jake

---------- Forwarded message ----------
From: Jake Carter <[EMAIL PROTECTED]>
Date: Wed, May 21, 2008 at 9:14 AM
Subject: Re: Trying to understand -- please help...
To: john darnell <[EMAIL PROTECTED]>


Ok, I'm somewhat of a beginner myself, but here's what I see.
First off: Your array is being 'emptied' because [NSArray arrayWithObjects:]
returns an autoreleased array. You should retain this like so [[NSArray
arrayWithObjects:blah] retain];

Second, you don't need to alloc and init the variable before using
arrayWithObjects.
Change this:
cityArray = [[NSArray alloc] init]; // This is creating an empty array.
  NSString *c0 = @"New York";  //Ten NSString objects created here
  ...
  NSString *c9 = @"Virginia Beach";
  cityArray = [NSArray arrayWithObjects: c0, ...c9, nil]; // When you do
this, you're not releasing the empty array that was there, so technically
you have a memory leak.
  return self;

to this:
cityArray = [[NSArray arrayWithObjects:@"New York", ..., @"Virgina Beach",
nil] retain];

Hope this helps.

:// Jake



On Wed, May 21, 2008 at 9:05 AM, john darnell <[EMAIL PROTECTED]>
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 PROTECTED]
>
> Trivia Question:  In SciFi Channel's hit series, FARSCAPE, who played
> the voice of Pilot?
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/jake%40thinginc.net
>
> This email sent to [EMAIL PROTECTED]
>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to