未找到实例方法callDelegateOnMainThread

时间:2014-03-25 17:44:33

标签: ios objective-c game-center gamekit

我收到错误instance method '-callDelegateOnMainThread:withArg:error:' not found我该怎么办?我将一些成就代码合并到Ray Wenderlichs回合制多人游戏代码中。

这些功能是以下界面的一部分:

@interface GCTurnBasedMatchHelper : NSObject <GKTurnBasedMatchmakerViewControllerDelegate, GKTurnBasedEventHandlerDelegate, GKAchievementViewControllerDelegate, MFMessageComposeViewControllerDelegate> {
    BOOL gameCenterAvailable;
    BOOL userAuthenticated;
    UIViewController *presentingViewController;
    NSMutableDictionary* earnedAchievementCache;
    GKTurnBasedMatch *currentMatch;
    //id <GCTurnBasedMatchHelperDelegate> delegate;
}

以下是功能

- (void) submitAchievement: (NSString*) identifier percentComplete: (double) percentComplete
{
    //GameCenter check for duplicate achievements when the achievement is submitted, but if you only want to report 
    // new achievements to the user, then you need to check if it's been earned 
    // before you submit.  Otherwise you'll end up with a race condition between loadAchievementsWithCompletionHandler
    // and reportAchievementWithCompletionHandler.  To avoid this, we fetch the current achievement list once,
    // then cache it and keep it updated with any new achievements.
    if(self.earnedAchievementCache == NULL)
    {
        [GKAchievement loadAchievementsWithCompletionHandler: ^(NSArray *scores, NSError *error)
        {
            if(error == NULL)
            {
                NSMutableDictionary* tempCache = [NSMutableDictionary dictionaryWithCapacity: [scores count]];
                for (GKAchievement* score in scores)
                {
                    [tempCache setObject: score forKey: score.identifier];
                }
                self.earnedAchievementCache = tempCache;
                [self submitAchievement: identifier percentComplete: percentComplete];
            }
            else
            {
                //Something broke loading the achievement list.  Error out, and we'll try again the next time achievements submit.
                [self callDelegateOnMainThread: @selector(achievementSubmitted:error:) withArg: NULL error: error];
            }

        }];
    }
    else
    {
         //Search the list for the ID we're using...
        GKAchievement* achievement = [self.earnedAchievementCache objectForKey: identifier];
        if(achievement != NULL)
        {
            if((achievement.percentComplete >= 100.0) || (achievement.percentComplete >= percentComplete))
            {
                //Achievement has already been earned so we're done.
                achievement= NULL;
            }
            achievement.percentComplete= percentComplete;
        }
        else
        {
            achievement = [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];
            achievement.percentComplete= percentComplete;
            //Add achievement to achievement cache...
            [self.earnedAchievementCache setObject: achievement forKey: achievement.identifier];
        }
        if(achievement != NULL)
        {
            //Submit the Achievement...
            [achievement reportAchievementWithCompletionHandler: ^(NSError *error)
            {
                 [self callDelegateOnMainThread: @selector(achievementSubmitted:error:) withArg: achievement error: error];
            }];
        }
    }
}


- (void) achievementSubmitted: (GKAchievement*) ach error:(NSError*) error;
{
    if((error == NULL) && (ach != NULL))
    {
        if (ach.percentComplete == 100.0) {
            //UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Achievement Earned!"
            //                      message:(@"%@",ach.identifier)
            //                      delegate:nil
            //                      cancelButtonTitle:@"OK"
            //                      otherButtonTitles:nil];
            //[alert show];
            //[alert release];
            TRACE("achievement submitted %s\n", [ach.identifier UTF8String]);
        }
    }
    else
    {
        // Achievement Submission Failed.
        printf("Achievement Submission Failed\n");
    }
}

2 个答案:

答案 0 :(得分:2)

看起来这是您正在寻找的缺失代码。只需将其放在GCTurnBasedMatchHelper.m文件中。

- (void) callDelegateOnMainThread: (SEL) selector withArg: (id) arg error: (NSError*) err
{
    dispatch_async(dispatch_get_main_queue(), ^(void)
                   {
                       [self callDelegate: selector withArg: arg error: err];
                   });
}

- (void) callDelegate: (SEL) selector withArg: (id) arg error: (NSError*) err
{
    assert([NSThread isMainThread]);
    if([delegate respondsToSelector: selector])
    {
        if(arg != NULL)
        {
            [delegate performSelector: selector withObject: arg withObject: err];
        }
        else
        {
            [delegate performSelector: selector withObject: err];
        }
    }
    else
    {
        NSLog(@"Missed Method");
    }
}

答案 1 :(得分:0)

只需添加不存在的方法。