将事件侦听器添加到回合制 ios 游戏时发出警告
warning when adding event listener to turn based ios game
这是gamekithelper.m
中的代码
- (void)authenticateLocalPlayer
{
//1
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
//add a weak local player
__weak GKLocalPlayer *blockLocalPlayer = localPlayer;
if (localPlayer.isAuthenticated) {
[[NSNotificationCenter defaultCenter] postNotificationName:LocalPlayerIsAuthenticated object:nil];
return;
}
//2
localPlayer.authenticateHandler =
^(UIViewController *viewController, NSError *error) {
//3
[self setLastError:error];
if(viewController != nil) {
//4
[self setAuthenticationViewController:viewController];
*******problem code************
// add event handler
[blockLocalPlayer registerListener:self];
} else if([GKLocalPlayer localPlayer].isAuthenticated) {
//5
_enableGameCenter = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:LocalPlayerIsAuthenticated object:nil];
*******problem code************
// add event handler
[blockLocalPlayer registerListener:self];
} else {
//6
_enableGameCenter = NO;
}
};
}
这里是gamekithelper.h
中的@interface
@interface GameKitHelper : NSObject <GKTurnBasedMatchmakerViewControllerDelegate, GKTurnBasedEventListener>
这是警告
Sending 'GameKitHelper *const __strong' to parameter of incompatible type 'id<GKLocalPlayerListener>'
我觉得我犯了一个很容易解决的错误,但我不知道是什么,你们中的任何人都可以帮助我吗?
额外信息
- 我在 spritekit 中这样做
[blockLocalPlayer registerListener:self];
此方法接受符合协议 GKLocalPlayerListener
的对象。正如您在界面中看到的那样,GameKitHelper
并未声明它符合 GKLocalPlayerListener
协议。
您应该将符合性声明添加到 GKLocalPlayerListener
,方法是更改此:
@interface GameKitHelper : NSObject <GKTurnBasedMatchmakerViewControllerDelegate, GKTurnBasedEventListener>
对此:
@interface GameKitHelper : NSObject <GKLocalPlayerListener, GKTurnBasedMatchmakerViewControllerDelegate, GKTurnBasedEventListener>
这是gamekithelper.m
中的代码- (void)authenticateLocalPlayer
{
//1
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
//add a weak local player
__weak GKLocalPlayer *blockLocalPlayer = localPlayer;
if (localPlayer.isAuthenticated) {
[[NSNotificationCenter defaultCenter] postNotificationName:LocalPlayerIsAuthenticated object:nil];
return;
}
//2
localPlayer.authenticateHandler =
^(UIViewController *viewController, NSError *error) {
//3
[self setLastError:error];
if(viewController != nil) {
//4
[self setAuthenticationViewController:viewController];
*******problem code************
// add event handler
[blockLocalPlayer registerListener:self];
} else if([GKLocalPlayer localPlayer].isAuthenticated) {
//5
_enableGameCenter = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:LocalPlayerIsAuthenticated object:nil];
*******problem code************
// add event handler
[blockLocalPlayer registerListener:self];
} else {
//6
_enableGameCenter = NO;
}
};
}
这里是gamekithelper.h
中的@interface@interface GameKitHelper : NSObject <GKTurnBasedMatchmakerViewControllerDelegate, GKTurnBasedEventListener>
这是警告
Sending 'GameKitHelper *const __strong' to parameter of incompatible type 'id<GKLocalPlayerListener>'
我觉得我犯了一个很容易解决的错误,但我不知道是什么,你们中的任何人都可以帮助我吗?
额外信息
- 我在 spritekit 中这样做
[blockLocalPlayer registerListener:self];
此方法接受符合协议 GKLocalPlayerListener
的对象。正如您在界面中看到的那样,GameKitHelper
并未声明它符合 GKLocalPlayerListener
协议。
您应该将符合性声明添加到 GKLocalPlayerListener
,方法是更改此:
@interface GameKitHelper : NSObject <GKTurnBasedMatchmakerViewControllerDelegate, GKTurnBasedEventListener>
对此:
@interface GameKitHelper : NSObject <GKLocalPlayerListener, GKTurnBasedMatchmakerViewControllerDelegate, GKTurnBasedEventListener>