Incompatible block pointer types sending 'void (^)(bool)' to parameter of type 'void(^)()'
我正在努力解决我正在做的回调函数上的此问题。
这是我定义全局块的方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #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的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #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 |
我在另一个这样的类的方法中使用该块:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | -(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 } } |
任何帮助表示赞赏。
谢谢,
最后,我找出了问题所在!
Cocos2D库中的另一个文件中有一个全局定义,该全局定义与该类的initWithBlock方法冲突!
我只是简单地重命名了init方法,就解决了问题,但是却浪费了一天的时间:-(
1 2 3 4 | /** initialized the action with the specified block, to be used as a callback. The block will be"copied". */ -(id) initWithBlock:(void(^)())block; |
谢谢你的帮助...