2014-03-25 57 views
0

我收到錯誤instance method '-callDelegateOnMainThread:withArg:error:' not found我應該在這裏做什麼?我將一些成就代碼合併到Ray Wenderlichs基於多人遊戲的代碼中。找不到實例方法callDelegateOnMainThread

的功能如下界面的一部分:

@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

看起來這是你正在尋找丟失的代碼。把它放在你的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"); 
    } 
} 
+0

那麼,如果callDelegateOnMainThread丟失,那麼callDelegate也不是不太可能。 :-)但是,錯誤信息恰恰說明了這一點。 –

+0

在上面添加了callDelegate。 – vincentjames501

+0

謝謝,只需要用自己替換代理,它就可以工作。我期待callDelegateOnMainThread繼承自某些東西,並且我還在做其他事情。我從來沒有想到這是一個用戶功能。 – Rasterman

0

只需添加不存在的方法即可。