Clarifying the devowelizer example


#1

In the example block “devowelizer”, we’re using three arguments. I get what they are, but I can’t see how the 2nd is being used:

id string is our target array

BOOL *stop is a flag we can turn on to stop the block and force an early return

Its the middle one, NSUInterger I can’t find a purpose for:

NSUInterger i I can’t see where it’s used…is it because we’re using fast enumeration on the array?


#2

Pretty close…

id string is one item in our array (this block will be executed for every item in the array – this will change each time).

NSUInteger i is the index of ‘string’ in our array. So it will be zero the first time, one the next, two, three…


#3

I purchased the book a couple weeks ago and almost done. So far so good but I have a question that seems similar to what is being asked here so figured I would tack on.

I understand that “NSUInteger i” being defined in the block is the array index for iteration through the array but where does it actually get set? I assume somewhere in the method enumerateObjectsUsingBlock?

When I looked at the NSArray.h file I see the enumerateObjectsUsingBlock method specifies “NSUInteger idx” for an argument but I don’t think I would of ever figured out that it is set “automatically”. I think I am missing something.

Thanks so much.


#4

That’s right. We hand enumerateObjectsUsingBlock() a block to execute once for each object in the array. For each execution, our block will be executed, and three parameters will be passed in: the current object, the index of the current object, and a pointer to a boolean.

The first two are set by enumerateObjectsUsingBlock() prior to passing them into our block. The last (the BOOL*), is a reference that we can fill in, as per the Pass By Reference chapter in the book.

Does that help?


#5

Thank you.

I guess I was getting confused (in reading the libraries) when say a method (e.g. enumerateObjectsUsingBlock() ) would require me to send along the input arguments vs the method coming up with them itself. So for example we can turn the BOOL on and off but it sets the NSUInteger for the index itself. Not sure if I was looking at the docs myself I would of realized that. Not sure if that makes sense.

Thanks again.


#6

It does indeed make sense. While the docs are by and large thorough, not all are as clear as they could be on some points. Which is why it’s nice to have a place to go to confirm suspicions. :slight_smile: