• 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
Re: Trying to understand -- please help...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Subject: Re: Trying to understand -- please help...
  • From: João Pavão <email@hidden>
  • Date: Wed, 21 May 2008 17:29:14 +0100

Also, since the initializer methods of the superclasses aren't invoked automatically for you, you should code your initializers using a pattern along the lines of:

- init
{
if (self = [super init]) {
// ... initialization statements for the instance variables of this class ...
}
return self;
}



-- João Pavão



On 2008/05/21, at 17:05, 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


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

  • Prev by Date: RE: Trying to understand -- please help...
  • 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