12

Is it possible to have a static variable of a "block type"?

I have a class that only does stuff in static methods. Upon execution of those methods i'm calling statusChangedBlock. Just for that i create a shared instance of the class, and use its single block property. I wonder if it's possible to have a static block variable; so i wouldn't have to create a instance with a single property, just for notifying that my status changed.

I know there is an option of NSNotification, but i don't like using it, with some rare exceptions.

...this question somehow sounds stupid, i can't tell why. I hope someone points that out.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
user1244109
  • 2,166
  • 2
  • 26
  • 24

2 Answers2

11

to declare a static variable of block type

typedef ReturnType (^MyBlockType)(ArgumentType, ArgumentType2);
static MyBlockType myblock;
static MyBlockType myblock2;

or

static ReturnType (^myblock)(ArgumentType, ArgumentType2);
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
  • 2
    `static MyBlockType myAssignedBlock = ^ReturnType(ArgumentType arg1,ArgumentType2 arg2){...do something...};` works fine. – Wil Macaulay Jul 27 '14 at 18:07
0

A block type variable is actually a pointer, similar to an object. You can have a static block variable, but you must assign its value at runtime, perhaps using a dispatch_once block.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Holly
  • 5,270
  • 1
  • 24
  • 27