Re: newbie : init ?
Re: newbie : init ?
- Subject: Re: newbie : init ?
- From: Robert Goldsmith <email@hidden>
- Date: Wed, 12 Jun 2002 10:22:31 +0100
On Wednesday, June 12, 2002, at 09:01 am, Famille
GOUREAU-SUIGNARD wrote:
>
I think I should override the init function of NSTextField, but I think
>
- (id) init
>
{
>
listeItem = [[NSMutableArray alloc] initWithCapacity:0];
>
return self ;
>
}
>
>
is not good.
>
Why ?
The first thing you need to do in an init method is init the
superclass you inherit from (in this case NSTextField) as it is
likely to have lots of things it needs to setup. To do this,
before the listeItem line, add:
[super init];
This will fix the init method.
However, you don't have any way to set the array either. There
are two ways to do this, at init or afterwards. If you want to do
it afterwards, use 'setters' and 'getters' e.g.
-(void)setArrayValue:(NSArray *)newArray
{
[listeItem autorelease];
listeItem=[newArray mutableCopy]; //you don't need to copy,
you could simply retain the newArray
}
and
-(NSMutableArray *)getArrayValue
{
return listeItem;
}
If you wish to be able to set the array when you init the object,
you can also create a new init method which takes the array as a
parameter:
-(id)initWithArrayValue:(NSArray *)newArray
{
[super init];
listeItem=[newArray retain];
return self;
}
Lastly, if you are retaining the array, which is recommended to
make sure it does not disappear unexpectedly, you also need to
release it when your object is destroyed. You can do this in the
equivalent of the init method called dealloc. e.g.
-(void)dealloc
{
[listeItem release];
[super dealloc];
}
as you can see, just like you have to allow the superclass a
chance to set itself up, you also need to tell it to clean up
ready to be released :)
Hope all this helps!
Robert
---
GnuPG public key:
http://www.Far-Blue.co.uk/RSGoldsmith.asc
[demime 0.98b removed an attachment of type application/pgp-signature which had a name of PGP.sig; charset=US-ASCII]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >newbie : init ? (From: Famille GOUREAU-SUIGNARD <email@hidden>) |