Objective-c – type instead of insertion characters between variables, enclose in parentheses

I am reading Apple’s documentation and I saw something similar (void(^)(void)). Can someone explain the meaning of this sentence? ^It’s XOR, right? Void XOR Void does not make much sense to me?

There is something similar (void(^)(BOOL complete))

These are blocks that add anonymous functions and function objects to Objective-C. See, for example, Introducing Blocks and Grand Central Dispatch:

Block objects (informally, “blocks”) are an extension to C, as well as Objective-C and C++, that make it easy for programmers to define self-contained units of work. Blocks are similar to — but far more powerful than — traditional function pointers. The key differences are:

  • Blocks can be defined inline, as “anonymous functions.”
  • Blocks capture read-only copies of local variables, similar to “ closures” in other languages

Declare a block variable:

void (^my_block)(void); 

Assign a block object to it:

my_block = ^(void){ printf("hello world
"); };

Call it:

my_block(); // prints "hello world
"

Accept a block as a parameter:

- (void)doSomething:(void (^)(void))block ;

Use this method with inline blocks:

[obj doSomeThing:^(void){ printf("block was called"); }];

I am reading Apple's documentation and I saw something similar (void(^)(void)). Can someone explain the meaning of this sentence? ? ^It's XOR, right? Void XOR Void does not make much sense to me?

There are similar things (void(^)(BOOL complete))

These are additions to Objective-C Blocks of anonymous functions and function objects. See for example Introducing Blocks and Grand Central Dispatch:

Block objects (informally, “blocks”) are an extension to C, as well as Objective-C and C++, that make it easy for programmers to define self-contained units of work. Blocks are similar to — but far more powerful than — traditional function pointers. The key differences are:

  • Blocks can be defined inline, as “anonymous functions.”
  • Blocks capture read-only copies of local variables, similar to “closures” in other languages

Declare a block variable:

void (^my_block)(void);

Assign a block object to it:< /p>

my_block = ^(void){ printf("hello world
"); };

Call it:

< /p>

my_block(); // prints "hello world
"

Accept block as a parameter:

- (void)doSomething :(void (^)(void))block;

Use this method with inline blocks:

[obj doSomeThing:^(void ){ print f("block was called"); }];

Leave a Comment

Your email address will not be published.