Question

I am trying to use block in macro definition but without a luck.

Here is the code:

#define IPHONE_OR_IPAD_BLOCK(void(^iphoneBlock)(void), void(^ipadBlock)(void)) ({ \
    if (iphoneBlock && (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)) \
    { \
        iphoneBlock(); \
    } \
    else if (ipadBlock) \
    { \
        ipadBlock(); \
    } \
})

and it doesn't seem to compile.

How would you suggest using blocks in macro?

UPDATE: Thanks for quick reply to Ramy Al Zuhouri, I'll stick with static function instead as it gives more type checking:

NS_INLINE void DoIphoneOrIpadBlock(void (^iphoneBlock)(void), void (^ipadBlock)(void))
{
    if (iphoneBlock && (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone))
    {
        iphoneBlock();
    }
    else if (ipadBlock)
    {
        ipadBlock();
    }
}
Was it helpful?

Solution

In a macro there isn't any parameter check, change it this way:

#define IPHONE_OR_IPAD_BLOCK(iphoneBlock, ipadBlock) ({ \
    if (iphoneBlock && (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)) \
    { \
        iphoneBlock(); \
    } \
    else if (ipadBlock) \
    { \
        ipadBlock(); \
    } \
})

If it gets used with wrong parameters you'd probably get a syntax error.
If you don't like this behaviour you could change it with an inline function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top