Re: Menu Mystery
Re: Menu Mystery
- Subject: Re: Menu Mystery
- From: Sherm Pendley <email@hidden>
- Date: Fri, 11 Jun 2004 01:30:29 -0400
On Jun 11, 2004, at 12:10 AM, Jerry LeVan wrote:
Here is the destroy code:
-(IBAction) updateScriptsMenu:(id)sender
{
int i;
NSMenu * zMenu = [sender menu];
int menuSize = [ zMenu numberOfItems];
NSLog(@"menusize: %d",menuSize);
for( i=3 ; i< menuSize ; i++) {
NSLog(@"I:%d Item:%@",i, [[zMenu itemAtIndex:i] title]);
[zMenu removeItemAtIndex:i];
}
}
...
The Items are not being removed in the proper order and the submenu is
skipped over,
each time I invoke the function about half the items are removed... (
I want to
keep the first three items, thats why the index starts at three.)
Each time through the loop, you increment i. But each time you remove
something from the array, everything above it is shifted down by one,
and the index of the last item reduced by one. The net effect is that
you remove every other item, and when you get about halfway done, i is
larger than the index of the last remaining item in the array.
The best solution is to loop downwards from menuSize to 3, calling
removeLastObject each time through the loop. For many mutable array
implementations, removeLastObject takes a fixed amount of time (O),
whereas the time taken to remove an item at an arbitrary index is a
function of the number of items above it (On). (That's why
removeLastObject is a primitive method in the cluster, so that those
implementing their own data stores can provide this optimization.)
sherm--
_______________________________________________
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: | |
| >Menu Mystery (From: Jerry LeVan <email@hidden>) |