SwiftUI GKLeaderboard loadEntries

SwiftUI GKLeaderboard loadEntries

我想在我的 SwiftUI 应用程序中添加排行榜。
我找不到任何使用 loadEntries 加载排行榜值的示例。 我尝试了以下...

let leaderBoard: GKLeaderboard = GKLeaderboard()
leaderBoard.identifier = "YOUR_LEADERBOARD_ID_HERE"
leaderBoard.timeScope = .allTime
leaderBoard.loadScores { (scores, error) in ...

这会导致以下警告:

使用 loadEntriesForPlayerScope 会导致以下警告:

使用loadEntries我不知道如何指定排行榜标识符。

我可能说的很明显,但你看过 WWDC20 视频了吗? 通常当有这样的大变化时,他们会在当年的 WWDC 期间进行报道。

Tap into Game Center: Leaderboards, Achievements, and Multiplayer

Tap into Game Center: Dashboard, Access Point, and Profile

我没看过视频,但是 documentation eludes that identifier might be replaced by var baseLeaderboardID: String

这是可能方法的简单演示 - 将所有内容放入视图模型并在视图中加载分数。

import GameKit

class BoardModel: ObservableObject {
    private var board: GKLeaderboard?
    @Published var localPlayerScore: GKLeaderboard.Entry?
    @Published var topScores: [GKLeaderboard.Entry]?
    
    func load() {
        if nil == board {
            GKLeaderboard.loadLeaderboards(IDs: ["YOUR_LEADERBOARD_ID_HERE"]) { [weak self] (boards, error) in
                self?.board = boards?.first
                self?.updateScores()
            }
        } else {
            self.updateScores()
        }
    }
    
    func updateScores() {
        board?.loadEntries(for: .global, timeScope: .allTime, range: NSRange(location: 1, length: 10),
            completionHandler: { [weak self] (local, entries, count, error) in
            DispatchQueue.main.async {
                self?.localPlayerScore = local
                self?.topScores = entries
            }
        })
    }
}

struct DemoGameboardview: View {
    @StateObject var vm = BoardModel()
    var body: some View {
        List {
            ForEach(vm.topScores ?? [], id: \.self) { item in
                HStack {
                    Text(item.player.displayName)
                    Spacer()
                    Text(item.formattedScore)
                }
            }
        }
        .onAppear {
            vm.load()
        }
    }
}