• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Fwd: Trying to understand -- please help...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Fwd: Trying to understand -- please help...


  • Subject: Fwd: Trying to understand -- please help...
  • From: "Jake Carter" <email@hidden>
  • Date: Wed, 21 May 2008 09:16:24 -0700

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


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


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@hidden>
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

  • Follow-Ups:
    • RE: Trying to understand -- please help...
      • From: "john darnell" <email@hidden>
References: 
 >Trying to understand -- please help... (From: "john darnell" <email@hidden>)

  • Prev by Date: Re: Writing Cocoa framework's for iPhone
  • Next by Date: Re: Trying to understand -- please help...
  • Previous by thread: Re: Trying to understand -- please help...
  • Next by thread: RE: Trying to understand -- please help...
  • Index(es):
    • Date
    • Thread