0

gamecenterhelper.mからすべての一致を返すパブリックメソッドを作成しています

私はこれを持っています:

+(NSArray *)retrieveMatchesWithMatchData {
    __block NSArray *myMatches = nil;
    [GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error) {
        if (error) {
            NSLog(@"There was an error loading matches");
        }
        else {
            myMatches = matches;
        }
    }];
    return myMatches;
}

しかし、アクティブな一致がある場合でも、これを呼び出すとnullが返されます。呼び出しメソッドは次のようになります。

NSLog(@"My matches: %@",[GameCenterHelper retrieveMatchesWithMatchData]);

御時間ありがとうございます!

4

1 に答える 1

0

それがブロックの性質です。ブロックは非同期で実行されます。GameCenterの一致を同期的にロードすることはできません。これをインスタンスメソッドにしましょう:

-(void)retrieveMatchesWithMatchData {
    [GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error) {
        if (error) {
            NSLog(@"There was an error loading matches");
        }
        else {
            [self matchesLoaded:matches];
        }
    }];
}

次に、このメソッドで一致を処理します。

-(void)matchesLoaded:(NSArray *)matches {
    //do something with your matches
}

編集:

あなたがやりたいことをする簡単な方法があります。Appleの標準ViewControllerを使用して、現在の一致を表示できます。

GKMatchRequest *request = [[GKMatchRequest alloc] init]; 
request.minPlayers = minPlayers;     
request.maxPlayers = maxPlayers;

GKTurnBasedMatchmakerViewController *mmvc = 
[[GKTurnBasedMatchmakerViewController alloc] 
 initWithMatchRequest:request];
mmvc.turnBasedMatchmakerDelegate = self;
mmvc.showExistingMatches = YES;
[self presentViewController:mmvc animated:NO completion:nil];
于 2013-03-14T22:01:04.457 に答える