• 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: NSEnumerator
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSEnumerator


  • Subject: Re: NSEnumerator
  • From: Andy Lee <email@hidden>
  • Date: Fri, 27 Jan 2006 15:54:52 -0500

Did you not get my reply the first time you posted the question?

--Andy

Begin forwarded message:
From: Andy Lee <email@hidden>
Date: January 26, 2006 10:31:49 PM EST
To: email@hidden
Subject: Re: -doSomethingTrivial: (was empty subject line)

On Jan 26, 2006, at 9:52 PM, Adam Johnson wrote:
-(void)doSomethingTrivial:(id)sender;
{
	NSEnumerator *enumerator = [items objectEnumerator];
	NSImage *aCell;
	while(aCell = [enumerator nextObject]);
	{
		int index = [items indexOfObject:aCell];
		NSLog(@"%i %i",[items indexOfObject:aCell],[items count]);
	}
}

The NSLog() prints something in the 4 billion ballpark. Also, the enumeator only goes through the first object. What am I doing wrong?

Get rid of the semicolon in this line:

while(aCell = [enumerator nextObject]);

It causes you to have a loop with an empty body. Thus it loops doing nothing until aCell is nil.


The block that follows is not the body of the loop; it is simply executed sequentially after the while loop. Since aCell is nil at this point, -indexOfObject: returns NSNotFound, which gets printed as that 4 billion number.

Note that you also have extra semicolons after each method signature and before the method body. I *believe* the compiler actually allows this on purpose for convenience in copying and pasting from your .h file to your .m file.

A couple of other comments:

* Your -init method leaks memory. The NSImages that you alloc/init should be autoreleased.

* You shouldn't use "aCell" as the name of an NSImage variable.

* -indexOfObject: returns an unsigned int, so technically you should use %u instead of %i in your NSLog, but in practice it wouldn't have mattered if the loop didn't have that bug.

--Andy


_______________________________________________
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


References: 
 >NSEnumerator (From: Adam Johnson <email@hidden>)

  • Prev by Date: Re: BEEP protocol
  • Next by Date: Re: I think I have found a big bug in NSBrowser
  • Previous by thread: Re: NSEnumerator
  • Next by thread: Showing a sheet right after a file was loaded
  • Index(es):
    • Date
    • Thread