Reza GN
Reza GN

Reputation: 49

Incompatible block pointer types sending 'void (^)(bool)' to parameter of type 'void(^)()'

I am struggling to fix this problem on a callback function that I am making. Here is how I've defined my global block:

#import <Foundation/Foundation.h>
#import "cocos2d.h"

typedef void (^RestartBlock)(bool);
RestartBlock block = ^(bool restart)
{
    if (restart) {
        // restart
    }
    else
    {
        // continue
    }
};

@interface RestartDialogLayer : CCLayer
{
    RestartBlock m_block;
    bool    m_bRestart;
}

-(id) initWithBlock:(RestartBlock)block;
-(void) restartButtonPressed:(id)sender;
-(void) resumeButtonPressed:(id)sender;

@end

Implementation of RestartDialogLayer:

#import "RestartDialogLayer.h"

@implementation RestartDialogLayer

-(id) initWithBlock:(RestartBlock)block
{
    if ((self = [super init]))
    {
        m_bRestart = YES;
        m_block = block;
    }
    return self;
}

-(void) restartButtonPressed:(id)sender
{
    m_bRestart = YES;
    m_block(m_bRestart);
    [self removeFromParentAndCleanup:YES];
}

-(void) resumeButtonPressed:(id)sender
{
    m_bRestart = NO;
    m_block(m_bRestart);
    [self removeFromParentAndCleanup:YES];
}

-(void) dealloc
{
    [super dealloc];
}

@end

and I use the block in a method of another class like this:

-(void) singlePlayerSceneSchedule:(ccTime) delta
{
    CCLOG(@"demoSceneSchedule MainMenuScene");
    [self unschedule:_cmd];

    bool gameLeftActive = [Globals sharedGlobals].gameLeftActive;

    if (gameLeftActive)
    {
        RestartDialogLayer* dialog = [[RestartDialogLayer node] initWithBlock:block];
    }
    else
    {
        // Start a new game
    }
}

Any help is appreciated.

Thanks,

Upvotes: 2

Views: 6157

Answers (1)

Reza GN
Reza GN

Reputation: 49

Finally, I figured out what the problem was!

There was a global definition in another file from the Cocos2D library that was clashing with my initWithBlock method of the class!

I simply renamed my init method and that fixed the problem but wasted a day of my time :-(

/** initialized the action with the specified block, to be used as a callback.
 The block will be "copied".
 */
-(id) initWithBlock:(void(^)())block;

Thanks for your help...

Upvotes: 2

Related Questions