我使用Swift的Objective-C。我幾乎沒有客觀的經驗。我試圖通過蘋果的例子訪問遊戲中心,並獲得在Swift中使用的前10名排行榜分數。然而,我堅持基本的Objective-C分配將檢索到的分數數據傳回給調用者。有人可以發佈代碼示例以瞭解如何處理此問題?塊內部的變量賦值
- (NSArray*) retrieveTopTenScores
{
GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
NSArray *temp = nil;
if (leaderboardRequest != nil)
{
leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
leaderboardRequest.timeScope = GKLeaderboardTimeScopeToday;
leaderboardRequest.identifier = @"Appid";
leaderboardRequest.range = NSMakeRange(1,10);
[leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
if (error != nil)
{
// Handle the error.
NSLog(@"error in score retrieval");
}
if (scores != nil)
{
temp = scores; //results to Variable is not assignable (missing __block type specifier)
}
}];
}
return temp;
}
聲明爲'__block的NSArray * TEMP =零;'所以警告會去,但返回值將是空的,因爲你的分數被上載異步方法,因此在得到結果之前執行return語句 –
最好是讓您的主應用程序控制器實現處理結果接收的方法,例如 –
這個設計存在一個根本問題,因爲當這個完成處理程序被稱爲temp時,它已經被返回。 temp只在本地範圍內,即在此方法內。我認爲你應該改變這個方法簽名爲 - (void)。該陣列沒有準備好立即返回。然後按照原樣繼續,但將(此NSArray數據)設置爲iVar或屬性,然後觸發另一個方法來通知返回的結果。 – Jef