Search code examples
objective-cobjective-c-blocksstatic-variables

Static block variable in Objective-C


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.


Solution

  • 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);