Search code examples
iossprite-kitgamekit

warning when adding event listener to turn based ios game


here is the code in 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;
         }
     };

}

here is the @interface in gamekithelper.h

@interface GameKitHelper : NSObject <GKTurnBasedMatchmakerViewControllerDelegate, GKTurnBasedEventListener>

here is the warning

Sending 'GameKitHelper *const __strong' to parameter of incompatible type 'id<GKLocalPlayerListener>'

I feel like I am making an easily fixable mistake but I can't figure out what it is, can any of you help me out?

Extra info

  • I am doing this in spritekit

Solution

  • [blockLocalPlayer registerListener:self];
    

    This method accepts an object that conforms to the protocol GKLocalPlayerListener. As you can see in your interface, GameKitHelper does not state that it conforms to the GKLocalPlayerListener protocol.

    You should add the declaration of conformance to GKLocalPlayerListener by changing this:

    @interface GameKitHelper : NSObject <GKTurnBasedMatchmakerViewControllerDelegate, GKTurnBasedEventListener>
    

    to this:

    @interface GameKitHelper : NSObject <GKLocalPlayerListener, GKTurnBasedMatchmakerViewControllerDelegate, GKTurnBasedEventListener>