我需要在 GKLeaderboard 上模拟 loadScores 请求。 为此我创建了扩展:
extension GKLeaderboard {
func testLoadScores(completion: @escaping ([GKScore]?, Error?) -> Void) {
...
self.maxRange = 100
}
}
但是是说
Cannot assign to property: 'maxRange' is a get-only property
那么我该如何覆盖这个属性呢? 链接到属性描述:here
最佳答案
我想问题是 maxRange
属性是在 loadScores(completionHandler:)
完成时自动设置的,你不应该尝试自己设置它
This property is invalid until a call to loadScores(completionHandler:) is completed. Afterward, it contains the total number of entries available to return to your game given the filters you applied to the query.
我想你应该使用 range
来获得(过滤)前 N 个分数
The range property is ignored if the leaderboard request was initialized using the init(playerIDs:) method. Otherwise, the range property is used to filter which scores are returned to your game. For example, if you specified a range of [1,10], after the search is complete, your game receives the best ten scores. The default range is [1,25]. The minimum index is 1. The maximum length is 100.
或者:如果您想获得所有分数并且分数超过 100 个,那么您似乎应该递归地加载范围越来越大的分数,例如 [1,100]、[101,200] .. . [1101, 1200].. 依此类推,直到您得到一个分数小于 100 的范围。但是实现分页是一个更好的主意,因为可能有太多的乐谱,以至于它们将花费太多时间来加载所有乐谱
关于ios - 如何在扩展中设置只读属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47975541/