• 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: -doSomethingTrivial: (was empty subject line)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: -doSomethingTrivial: (was empty subject line)


  • Subject: Re: -doSomethingTrivial: (was empty subject line)
  • From: Andy Lee <email@hidden>
  • Date: Thu, 26 Jan 2006 22:31:49 -0500

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: 
 >(no subject) (From: Adam Johnson <email@hidden>)

  • Prev by Date: Re: Fairly newbie question on strings
  • Next by Date: Getting control IDs at initialization
  • Previous by thread: (no subject)
  • Next by thread: NSObject's inverseForRelationshipKey: behavior
  • Index(es):
    • Date
    • Thread