Re: Method not seeing array as array
Re: Method not seeing array as array
- Subject: Re: Method not seeing array as array
- From: Guy English <email@hidden>
- Date: Wed, 2 Feb 2005 15:58:14 -0500
Michael Swan <email@hidden>
> NSArray *channelArray;
First, since you change the contents of this you should declare it as
an NSMutableArray. It's not actually the problem though because the
types are more for the programmer in Objective-C than the compiler.
> #import "AppController.h"
> - (id)init
> {
> [super init];
As Andrew pointed out you want to do self = [super init]; The reasons
are a little esoteric right now but it's a good habit to get into. If
you're curious you can look it up somewhere. I think Mmalc wrote a
stepwise article about it but I can't be certain.
This isn't actually the problem either so since self will, in fact, be valid.
> channelArray = [[NSMutableArray alloc] init];
Here you do the right thing - allocate a new mutable array. It's not
perfect because you declared channelArray to be a simple NSArray
though. Can't remember but I don't think you'll get a warning because
NSMutableArray is a subclass of NSArray so the assignment looks valid
to the compiler.
> //Have to do this all here for this method to see channelArray as an
> NSArray
> //putting this code in init or awakeFromNib and I get
> //[NSMachPort objectAtIndex:] selector not recgonized but other
> methods can tell that
> //channelArray is an NSArray no matter where the code is
Ok, as soon as you see wierd and wonderful objects complaining about
messages they don't like you should be thinking about dangling
pointers ... NSMachPort is not something you're likely to want to talk
to.
> channelArray = [NSArray arrayWithObjects:channel1, channel2, channel3,
> channel4, channel5, channel6, channel7, channel8, channel9, channel10,
> channel11, channel12, nil];
This is your problem. You're calling arrayWithObjects which does
create an array for you - unfortunately for you it's shortlived and
autoreleased next time through the run loop. That means you end up
with channelArray pointing to basically random memory and it just
happens, by chance, that it looks to the run time like an NSMachPort
which is why you get the exception. What you want to be doing is
probably:
[channelArray removeAllObjects];
[channelArrray addObjects: channel1, channel2, ..., etc];
Since you allocated channelArray before as an NSMutableArray you can
just clear it and add your objects back to it. So that's the problem
you're having.
Get rid of the the channelArray setup into since you already to it
(correctly) in the init method.
Hope that helps a little,
Guy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden