不兼容的块指针类型将'void(^)(bool)'发送到'void(^)()'类型的参数

时间:2012-12-26 01:55:10

标签: objective-c objective-c-blocks

我正在努力解决我正在制作的回调函数上的这个问题。 以下是我定义全局块的方法:

#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

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

我在另一个类的方法中使用了这个块:

-(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
    }
}

感谢任何帮助。

谢谢,

1 个答案:

答案 0 :(得分:1)

最后,我弄清楚问题是什么!

Cocos2D库中的另一个文件中存在一个全局定义,该文件与我的类的initWithBlock方法发生冲突!

我只是重命名了我的init方法并修复了问题但浪费了我一天的时间: - (

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

感谢您的帮助......