Reputation: 183
GKLeaderboardSet.loadLeaderboardSetsWithCompletionHandler(_ completionHandler: (([GKLeaderboardSet]?,NSError?) -> Void)?)
I'm trying to use this class function to retrieve the information of the leaderboard sets from Game Center.
var leaderboardSetsRetrived = [""]
GKLeaderboardSet.loadLeaderboardSetsWithCompletionHandler({ (leaderboardSets, error) -> Void in
leaderboardSetsRetrived = leaderboardSets
})
This is what I tried to do, but xcode says
"Cannot convert value of type '([String], _) -> Void' to expected argument type '(([GKLeaderboardSet]?, NSError?) -> Void)?"
What should I put for the parameters?
What Game Center Programming Guide told me to do:
Before you can load the leaderboards contained within a set, you must load the set itself. Retrieve the list of leaderboard sets for your game from Game Center and display their titles. Use the loadLeaderboardSetsWithCompletionHandler: method to retrieve the leaderboard sets for your app. You can then load the leaderboards for a specific leaderboard set using the loadLeaderboardsWithCompletionHandler: method.
@available(iOS 7.0, *)
public class GKLeaderboardSet : NSObject, NSCoding, NSSecureCoding {
public var title: String { get } // Localized set title.
public var groupIdentifier: String? { get } // set when leaderboardSets have been designated a game group; set when loadLeaderboardSetsWithCompletionHandler has been called for leaderboards that support game groups
public var identifier: String? // leaderboard set.
// Loads array with all sets for game
// Possible reasons for error:
// 1. Communications problem
// 2. Unauthenticated player
// 3. Set not present
@available(iOS 7.0, *)
public class func loadLeaderboardSetsWithCompletionHandler(completionHandler: (([GKLeaderboardSet]?, NSError?) -> Void)?)
// Loads array with all leaderboards for the leaderboardSet
// Possible reasons for error:
// 1. Communications problem
// 2. Unauthenticated player
@available(iOS 7.0, *)
public func loadLeaderboardsWithCompletionHandler(completionHandler: (([GKLeaderboard]?, NSError?) -> Void)?)
}
extension GKLeaderboardSet {
// Asynchronously load the image. Error will be nil on success.
public func loadImageWithCompletionHandler(completionHandler: ((UIImage?, NSError?) -> Void)?)
}
code from GKLeaderboardSet.h
Now my code is working.
GKLeaderboardSet.loadLeaderboardSetsWithCompletionHandler({ (leaderboard, error) -> Void in
})
But the function isn't receiving any parameters for some reason. I can't save the leaderboardSets
that's supposed to be retrieved to a variable that I created. According to the GKLeaderboard Class Reference:
Parameters:
completionHandler
A block that is called when the categories have been retrieved from the server.
The block receives the following parameters:
leaderboardSets
An array ofGKLeaderboardSet
objects that provides the leaderboard sets for your game. If an error occurred, this value may be non-nil. In this case, the array holds whatever data Game Kit was able to download before the error occurred.
error
If an error occurred, this error object describes the error. If the operation completed successfully, the value is nil.
Upvotes: 0
Views: 127
Reputation: 183
Fixed now. A mistake I made: leaderboardSetsRetrieved
should be [GKLeaderboardSet]
instead of a string array. Second, for some reason leaderboardSetsRetrived = leaderboardSets
does work, it's just that Xcode doesn't auto complete leaderboardSets
for some reason and I thought there's something wrong with the parameter.
Upvotes: 0