Re: Getting selected items in a matrix
Re: Getting selected items in a matrix
- Subject: Re: Getting selected items in a matrix
- From: Nicholas Shanks <email@hidden>
- Date: Sat, 23 Feb 2002 17:03:45 +0000
This reply is in two parts, (1) bugs in your code, and (2) a better way to
write the method:
On Saturday, February 23, 2002, at 04:46 pm, Peter Schols wrote:
- (NSString *)setDay:(id)sender
{
int i;
NSArray *array = [[NSArray alloc] init];
array = [dayButton selectedCells];
while (i < [array count]) {
if ([[array objectAtIndex:i] state] == NSOnState) {
// code to add the Tag of the selected item to another array
goes here }
}
}
1) You allocate an array with [[NSArray alloc] init], then throw away the
reference to it by calling array = [dayButton selectedCells]. You could
simply have put NSArray *array = [dayButton selectedCells]; which would
avoid a memory leak.
2) Instead of your while loop, I would use an enumerator:
{
NSCell *currentCell;
NSEnumerator *enumerator = [[dayButton selectedCells]
objectEnumerator];
while( currentCell = [enumerator nextObject] )
{
if( [currentCell state] == NSOnState )
{
// code to add the Tag of the selected item to another array
goes here
}
}
}
Something to note: is "dayButton" an NSMatrix, or a cell within it? The
name seems misleading to me.
Nicholas Shanks
--
http://nickshanks.com/
_______________________________________________
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.