Amazon.com Widgets Fast Enumeration
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 20, 2013, 12:51:10 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Program Examples
| | |-+  Chapter 15
| | | |-+  Fast Enumeration
Pages: [1]   Go Down
Print
Author Topic: Fast Enumeration  (Read 673 times)
delgreco
Newbie
*
Posts: 7


Email




« on: February 25, 2010, 06:48:16 PM »

Hopefully someone here can explain this to me:

Page 344 - Looking up someone in the address book
for (AddressCard *nextCard in book)
if ([[nextCard name] caseInsensitiveCompare: theName] == NSOrderedSame)
return nextCard;

return nil;
}

My question is why is there not an "else" statement between return nextCard and return nil.  To me, this code says that it will enumerate through all the objects in the array, if it finds a matching name, it will return the nextCard and it will also execute the return nil statement.  How is it possible to return the nextCard object then return nil??  My thinking is it has to be one or the other....if you can't find a matching name, then return nil.......but if you do find a matching name, then return the nextCard and only the nextCard.....

How can both nextCard and nil be assigned to the myCard object in the .m file??  
myCard = [ myBook lookup: @"stephen kochan" ];
« Last Edit: August 10, 2011, 10:35:08 AM by fujilla » Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: February 25, 2010, 06:51:55 PM »

Executing a return causes the method to immediately exit, returning the value of the expression that follows.  The first match that's made will return that match (nextCard) and terminate execution of the method.  If no match is made, the if will never be true, and the for loop will terminate.  Once the loops terminates, the next statement that follows the loop will be executed.  That is a return statement that will return the value nil.

Cheers,

Steve
« Last Edit: February 25, 2010, 06:59:16 PM by skochan » Logged
delgreco
Newbie
*
Posts: 7


Email




« Reply #2 on: February 25, 2010, 07:25:59 PM »

Great....thanks Stephen!!!!!!!
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.