简体   繁体   中英

Cannot update parse user details - Swift

I use below code on AppDelegate.swift to check if a user is logged in in order to change the default view controller.

 if let user = PFUser.currentUser()!["username"] {

        print("asldkeu \(user)")
        // Code to execute if user is logged in
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController")

        self.window?.rootViewController = viewController
        self.window?.makeKeyAndVisible()

    } else {
        // Default screen you set in info plist.
    }

Which works correctly, but the problem is that then I cannot update my online parse user data. Whenever I try to update any data on my Heroku Parse app I get below error code:

Error Domain=Parse Code=206 "cannot modify user Jpibm8rWyI" UserInfo={code=206, temporary=0, error=cannot modify user Jpibm8rWyI, NSLocalizedDescription=cannot modify user

I am using below code to update parse data:

if let games = PFUser.currentUser()?["gamesWon"]! {
            print("asldkeu server: \(games as! Int)")

            gamesWon = (games as! Int) + 1
                print("asldkeu update: \(gamesWon)")
            }


            PFUser.currentUser()?["gamesWon"] = gamesWon

            do {
                try PFUser.currentUser()!.save()
            } catch {
                print("asldkeu \(error)")
            }}

And if I use PFUser.currentUser()!.saveInBackground() I don't get an error, but the data isn't saved.

Any help how this can be solved?

You are not force unwrapping the user.

instead of PFUser.currentUser()?["gamesWon"] = gamesWon

use

PFUser.currentUser()!["gamesWon"] = gamesWon

Note that there is a ! rather than ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM