Discuss Swift.

Swift Documentation

Posts under Swift subtopic

Post

Replies

Boosts

Views

Activity

Game Center fetchSavedGames sometimes returns empty list of games, although it works correctly on the next tries
I have implemented the Game Center for authentication and saving player's game data. Both authentication and saving player's data works correctly all the time, but there is a problem with fetching and loading the data. The game works like this: At the startup, I start the authentication After the player successfully logs in, I start loading the player's data by calling fetchSavedGames method If a game data exists for the player, I receive a list of SavedGame object containing the player's data The problem is that after I uninstall the game and install it again, sometimes the SavedGame list is empty(step 3). But if I don't uninstall the game and reopen the game, this process works fine. Here's the complete code of Game Center implementation: class GameCenterHandler { public func signIn() { GKLocalPlayer.local.authenticateHandler = { viewController, error in if let viewController = viewController { viewController.present(viewController, animated: false) return } if error != nil { // Player could not be authenticated. // Disable Game Center in the game. return } // Auth successfull self.load(filename: "TestFileName") } } public func save(filename: String, data: String) { if GKLocalPlayer.local.isAuthenticated { GKLocalPlayer.local.saveGameData(Data(data.utf8), withName: filename) { savedGame, error in if savedGame != nil { // Data saved successfully } if error != nil { // Error in saving game data! } } } else { // Error in saving game data! User is not authenticated" } } public func load(filename: String) { if GKLocalPlayer.local.isAuthenticated { GKLocalPlayer.local.fetchSavedGames { games, error in if let game = games?.first(where: {$0.name == filename}){ game.loadData { data, error in if data != nil { // Data loaded successfully } if error != nil { // Error in loading game data! } } } else { // Error in loading game data! Filename not found } } } else { // Error in loading game data! User is not authenticated } } } I have also added Game Center and iCloud capabilities in xcode. Also in the iCloud section, I selected the iCloud Documents and added a container. I found a simillar question here but it doesn't make things clearer.
1
0
830
1d
Parameter Errors - procedural vs. optional
So I’m writing a program, as a developer would - ‘with Xcode.’ Code produced an error. The key values were swapped. The parameters suggested were ‘optional parameters variables.’ “var name: TYPE? = (default)” var name0: TYPE ============================= name0 = “super cool” ‘Name is not yet declared at this point provided with x - incorrect argument replace ExampleStruct(name:”supercool”) should be x - incorrect argument replace ExampleStruct(name0:”supercool”) ============================= In swift, there is a procedural prioritization within the constructor calling process. Application calls constructor. Constructor provides constructor signature. Signature requires parameters & throws an error if the params are not in appropriate order. - “got it compiler; thank you, very much” Typically, when this occurs, defaults will be suggested. Often the variable type. Ie String, Bool. such as: StructName(param1:Int64, param2:Bool) (Recently, I have seen a decline in @Apple’s performance in many vectors.) As stated before, the key value pairs were out of sequence. The optionals were suggested instead of the required parameters. This leads me to believe that there is an order of operations in the calling procedure that is being mismanaged. I.e. regular expression, matching with optional. This confuses these with [forced, required] parameters, and the mismanagement of ‘key: value’ pairs. this is a superficial prognosis and would like to know if anyone has any insight as to why this may occur. Could it be a configuration setting? Is it possibly the network I connected to bumped into something. Etc.. I appreciate any and all feedback. Please take into consideration the Apple developer forum, guidelines before posting comments. #dev_div
0
0
38
5h