要從調用的block中返回整數:
-(NSInteger)globalRecord
{
__block NSInteger globalRecord = 0;
[GKLeaderboard loadLeaderboardsWithCompletionHandler:^(NSArray *leaderboards, NSError *error) {
GKLeaderboard *globalTaps = nil;
for (GKLeaderboard *lb in leaderboards) if ([lb.category isEqualToString:kGlobalLeaderboard]) globalTaps = lb;
[globalTaps loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if ([scores count] != 0)
{
GKScore *topScore = [scores objectAtIndex:0];
globalRecord = topScore.value;
//42 here
}
}];
}];
//0 here
return globalRecord;
}
從游戲中心的排行榜返回最高分,需求是只要接收到分數方法就返回給我,但是沒返回成功。
比如在block中最高分是42,但是在block外值就是0。
請高手幫忙解答。
要獲取globalRecord
值應該使用一個回調。
-(void)globalRecord
{
__block NSInteger globalRecord = 0;
[GKLeaderboard loadLeaderboardsWithCompletionHandler:^(NSArray *leaderboards, NSError *error) {
GKLeaderboard *globalTaps = nil;
for (GKLeaderboard *lb in leaderboards) if ([lb.category isEqualToString:kGlobalLeaderboard]) globalTaps = lb;
[globalTaps loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
if ([scores count] != 0)
{
GKScore *topScore = [scores objectAtIndex:0];
globalRecord = topScore.value;
[self globalRecordRetrieved:globalRecord];
//42 here
}
}];
}];
}
- (void)globalRecordRetrieved:(NSInteger)record{
NSLog(@"%d",record); //42 here
}