via NedraI via CC License
A funny question
Here is the funny question:
Is there any object in Cocoa/Objective-C that is a object but you do not use
* to indicate it?
Now, think a few seconds before you read on: is there any such kind of objects?
NO? are you sure about that?
The answer is YES! Yeah, there is a such kind of object in Cocoa/Objective-C.
It is the Block object!
Block, a much more common name is Closure. As you can see from the wiki page, it is a non-standard extention to C/C++/Objective-C 2.0
Because it is an extension not only to Objective-C, but also to C/C++, so it has some special treatment.
Because Block has
To indicate a piece of code as a Block(or a Closure), it DOES need something like a pointer, but not a normal pointer. It act as a function pointer, but much more powerful.
So Apple introduced
^ as a replacement of
* to Block.
^ == *
As we can see,
^ is also a pointer, so why do we need another
* to pointer to a Block?
Use it as a method argument
typedef void (^MyBlock)(void);
Use it as a property
typedef void (^YourBlock)(void);
@property (nonatomic, copy) YourBlock block;
Use it as a function argument
typedef void (^HisBlock)(void);
void myfunction(HisBlock block);
Note that, none of above is using
* to indicate a block, because the
typedef has already indicated it is an object.
Of course, don’t forget to follow me @TonnyXu and Facebook