如何在扩展中设置只读属性?
How to set readonly property in extension?
我需要在 GKLeaderboard 上模拟 loadScores 请求。
为此,我创建了扩展名:
extension GKLeaderboard {
func testLoadScores(completion: @escaping ([GKScore]?, Error?) -> Void) {
...
self.maxRange = 100
}
}
但是说
Cannot assign to property: 'maxRange' is a get-only property
那么我该如何覆盖这个 属性?
Link 到 属性 的描述: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.
OR: 如果你想得到所有的分数,并且有超过 100 个分数,你似乎应该递归地加载分数,范围越来越大,比如 [1,100], [101,200] ... [1101, 1200].. 等等,直到你得到一个小于 100 分的范围。但是实施分页是一个更好的主意,因为可能有太多的乐谱,以至于他们将花费太多时间来加载它们
我需要在 GKLeaderboard 上模拟 loadScores 请求。 为此,我创建了扩展名:
extension GKLeaderboard {
func testLoadScores(completion: @escaping ([GKScore]?, Error?) -> Void) {
...
self.maxRange = 100
}
}
但是说
Cannot assign to property: 'maxRange' is a get-only property
那么我该如何覆盖这个 属性? Link 到 属性 的描述: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.
OR: 如果你想得到所有的分数,并且有超过 100 个分数,你似乎应该递归地加载分数,范围越来越大,比如 [1,100], [101,200] ... [1101, 1200].. 等等,直到你得到一个小于 100 分的范围。但是实施分页是一个更好的主意,因为可能有太多的乐谱,以至于他们将花费太多时间来加载它们