0

I think it's funny about the api

- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(KeyType key, ObjectType obj, BOOL *stop))block

I noticed the parameter stop.At first,I think it's a type of (BOOL *) I have never seen.But The document says

Discussion If the block sets *stop to YES, the enumeration stops.

It seems like the parameter called *stop,and I delete * will get an error.

I wonder Why?

Joe Zhow
  • 833
  • 7
  • 17
  • What do you mean by `I delete * will get an error`? I can answer why the paramter is `BOOL *` but I don't understand what do you mean with the second part of your question. – Majster Jul 27 '16 at 11:08

1 Answers1

1

I think this is a very basic question about the C language on which Objective-C is based. You aren't familiar with pointers?

stop is a parameter of the block. Its type is BOOL* which means "a pointer to BOOL".

Somewhere within the implementation of -enumerateKeysAndObjectsUsingBlock: is code which calls the block you supllied. That code has created storage for a BOOL value. It is passing the pointer to (a.k.a. address of) that storage to your block so that your block may, by writing through the pointer, modify the value at that storage location.

The statement *stop = YES; assigns the value YES to the BOOL pointed to by the stop variable. That's what I meant above by "writing through the pointer". In this way, the block has modified a variable of the caller's, which is how the caller knows that the block wants the enumeration to stop.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154