Amazon.com Widgets 1st Printing Chapter 15 ( Errata on indexOfObjectPassingTest page 363 )
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 05:08:42 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
|-+  Programming in Objective-C, 4th edition
| |-+  Errata
| | |-+  1st Printing Chapter 15 ( Errata on indexOfObjectPassingTest page 363 )
Pages: [1]   Go Down
Print
Author Topic: 1st Printing Chapter 15 ( Errata on indexOfObjectPassingTest page 363 )  (Read 834 times)
iOSro
Newbie
*
Posts: 5






« on: March 15, 2012, 09:55:52 AM »

Hi Steve,

( i just bought the book last week and i've noticed is the 1-st Printing dec 2011 )

I came across this rather nasty issue for which i have to admit i smashed my head against the wall ( presuming i am making mistakes somewhere and trying to figure it out ... ) and would like to try give an errata on this ...

I am referring here to page 363 to the piece of code mentioned in the book as follows :
Code: (Objective-C)
-(NSMutableArray *) lookupAll: (NSString *) theName
{
    NSMutableArray * matches = [NSMutableArray array];
    
    NSUInteger result =[book indexOfObjectPassingTest:
                         ^(id obj, NSUInteger idx, BOOL * stop)
                         {
                             if ( [[obj name] caseInsensitiveCompare:theName] == NSOrderedSame ) /* here is another small errata:
 in the book it reads =- NSOrderedSame but that takes anyone 1 sec to figure out it's a mistake, on contrary for what
i am about to present here*/
                             {                      
                                 [matches addObject:obj];
                                 return YES;
                             }
                             else
                                 return NO;
                         }];
    

    // see if we found any matches
    if ( [matches count] )
        return matches;
    else
        return nil;
}

When reading through the book, the above is an example of how you can build an array that will hold  all the indexes for which you can find matching names when searching inside the array of the AddressCards.
Now i can tell you ( i also found now when writing here that someone else found this similar issue in here http://classroomm.com/objective-c/index.php?topic=5949.0 ) that this is not working.

I just want to try to conclude that indeed indexOfObjectPassingTest method will always stop to 1-st index the block condition will occur.

However i kept smashed my head against the wall and thought since is written in the book and not on errata it must working, is just me not geting it right yet and falling to understand ... so i thought i found the problem by looking again at this code and see how the block is trying to modify a variable defined outside of the block without the ( double underscore )_ _ block, but then i asked myself why isn't throwing a compilation error anyways as i already learned in previous chapter about blocks ( on how they can modify a variable defiend outside ) ? What do i still miss more regarding this matter ?!

so 2 things please :
1) if i am right, this the 1-st important ( of course from my point of view ! ) errata  that could saved me 1 day  Roll Eyes
2) what do i miss please on the NSMutableArray matches variable defined outside of the block that is trying to be modified inside the block ( [matches addObject: obj] ) - why i can not get a compilation error as suspected because of the lack of __block on variable definition ?

@"thank you"
« Last Edit: March 15, 2012, 10:02:17 AM by iOSro » Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: March 15, 2012, 09:05:41 PM »

Yes, you're right.  I thought I had tested that.   The documentation for the method implies that the method goes through the entire array, stopping sooner if you set the BOOL stop value to YES.   But that's not the case.  Apparently, it also stops if you return YES from the block.  I'll be sure to update the text in the next printing.

As for #2, you're not changing the value of the variable "matches"; you are using its current value as the reference to the array and then adding objects to the array using the addObject: method.

Thanks,

Steve Kochan
« Last Edit: March 16, 2012, 08:32:37 AM by skochan » Logged
iOSro
Newbie
*
Posts: 5






« Reply #2 on: March 16, 2012, 05:40:39 AM »

Thank you Steve,

much appreciated.

@"thank you"
Logged
urbanlung
Newbie
*
Posts: 13






« Reply #3 on: June 28, 2012, 04:12:33 PM »

Hi Guys,

I've just completed a head banging as well. Eventualy sorted it by making both return values NO thus negating the BOOL condition (not shown in book) after the ^. On the positive side, unpicking stuff does help the learning process, I think.

Regards

Matt Smiley
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.