Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why most block use argument for iteration continue flag instead of return value?

For most iteration blocks, signatures are defined sort of:

void(^)(id obj, BOOL* stop)

It looks better using return value for stopping flag.

BOOL(^)(id obj)

However I believe there is a strong reason made them to decide to use current form instead of more short and convenient form. Why do they use argument for stop flag?

like image 807
eonil Avatar asked Oct 11 '22 12:10

eonil


1 Answers

Sure, it would work fine either way, but think of it this way: do you have to use the continue statement at the end of your for or while loop? No, of course not because it happens automatically. If you want to stop looping though, you can just use break.

Similarly with the block-based enumeration methods. You're not required to return YES to specify whether you want to continue enumeration or not because it will happen automatically. However, if you want to stop enumerating, you can simply set *stop to YES.

like image 188
kperryua Avatar answered Oct 13 '22 02:10

kperryua