从设备上的解析中保存用户 a/c 的详细信息以供离线使用。 Swift 3

Saving User a/c details from parse on device for offline use. Swift 3

我已经设置了解析和所有设置,注册可以根据需要完美运行。但在那之后,我无法在设备上取回用户详细信息以供离线使用。

一旦用户登录/注册,我就需要这些对象离线。

Class 姓名:User

对象:

  1. coverPhoto : Image.png(另存为文件)

  2. profilePicture : Image.png(另存为文件)

  3. usersUsername : 字符串

  4. username / email : 字符串

  5. firstName : 字符串

  6. middleName : 字符串

  7. lastName : 字符串

  8. mobileNumber : 字符串

  9. birthDate : 字符串

  10. gender : 字符串

  11. about : 字符串

我尝试使用 UserDefaults.standard.set(_variable name_, forKey: "String") 存储它,但没有成功。 :\

这是我反复得到的。

这是我写的:

@IBAction func loginButtonPressed(_ sender: AnyObject) {

    activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    activityIndicator.center = self.view.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = .gray
    activityIndicator.layer.zPosition = 1
    view.addSubview(activityIndicator)
    activityIndicator.startAnimating()
    UIApplication.shared.beginIgnoringInteractionEvents()

    if email.text == "" || password.text == "" {

        createAlert(title: "Error", message: "Please enter your username & password!")

        self.activityIndicator.stopAnimating()
        UIApplication.shared.endIgnoringInteractionEvents()

    } else {

        PFUser.logInWithUsername(inBackground: email.text!, password: password.text!, block: { (suser, error) in



            if error != nil {

                var displayErrorMessage = "Some Error Occured! Please Try Again Later!"

                if let errorMessage = error as? NSError {
                    displayErrorMessage = errorMessage.userInfo["error"] as! String
                }
                self.createAlert(title: "Dang it!", message: displayErrorMessage)

                self.activityIndicator.stopAnimating()
                UIApplication.shared.endIgnoringInteractionEvents()

            } else {

                self.performSegue(withIdentifier: "toMainFunction", sender: nil)

                let query = PFUser.query()

                query?.findObjectsInBackground(block: { (objects, error) in

                    if error != nil {

                        print(error)

                    } else if let users = objects {

                        for object in users {

                            if let user = object as? PFUser {

                                if user.objectId != PFUser.current()?.objectId {

                                    if let email = user.username {
                                        UserDefaults.standard.set(email, forKey: "email")
                                    }

                                    if let coverFile = user["coverPhoto"] {
                                        UserDefaults.standard.set(coverFile, forKey: "coverPhoto")
                                    }

                                    if let profileFile = user["profilePicture"] {
                                        UserDefaults.standard.set(profileFile, forKey: "profilePicture")
                                    }

                                    if let firstName = user["firstName"] {
                                        UserDefaults.standard.set(firstName, forKey: "firstName")
                                    }

                                    if let middleName = user["middleName"] {
                                        UserDefaults.standard.set(middleName, forKey: "middleName")
                                    }

                                    if let lastName = user["lastName"] {
                                        UserDefaults.standard.set(lastName, forKey: "lastName")
                                    }

                                    if let mobileNumber = user["mobileNumber"] {
                                        UserDefaults.standard.set(mobileNumber, forKey: "mobileNumber")
                                    }

                                    if let birthday = user["BirthDate"] {
                                        UserDefaults.standard.set(birthday, forKey: "BirthDate")
                                    }

                                    if let gender = user["gender"] {
                                        UserDefaults.standard.set(gender, forKey: "gender")
                                    }

                                    if let aboutYou = user["about"] {
                                        UserDefaults.standard.set(aboutYou, forKey: "about")
                                    }

                                    if let username = user["usersUsername"] {
                                        UserDefaults.standard.set(username, forKey: "username")
                                    }

                                    gloFirstName = UserDefaults.standard.object(forKey: "firstName") as! String
                                    gloMiddleName = UserDefaults.standard.object(forKey: "middleName") as! String
                                    gloLastName = UserDefaults.standard.object(forKey: "lastName") as! String
                                    gloMobNumber = UserDefaults.standard.object(forKey: "mobileNumber") as! String
                                    gloEmail = UserDefaults.standard.object(forKey: "email") as! String
                                    gloUName = UserDefaults.standard.object(forKey: "username") as! String
                                    gloBirthDate = UserDefaults.standard.object(forKey: "BirthDate") as! String
                                    gloGenderStr = UserDefaults.standard.object(forKey: "gender") as! String
                                    gloAbout = UserDefaults.standard.object(forKey: "about") as! String

                                    if let coverImageData = UserDefaults.standard.object(forKey: "coverPhoto"),
                                        let coverPhoto = UIImage(data: coverImageData as! Data){

                                        gloCoverImage = coverPhoto

                                    }


                                    if let profileImageData = UserDefaults.standard.object(forKey: "profilePicture"),
                                        let profilePicture = UIImage(data: profileImageData as! Data){
                                        gloProfilePicture = profilePicture

                                    }


                                }

                            }

                        }

                    }

                })

                self.activityIndicator.stopAnimating()
                UIApplication.shared.endIgnoringInteractionEvents()

            }

        })

    }

}

我在这里陷入困境。

编辑:

编辑#2:

高亮代码:

 let query = PFUser.query()

                query?.findObjectsInBackground(block: { (objects, error) in

                    if error != nil {

                        print(error)

                    } else if let users = objects {

                        for object in users {

                            if let user = object as? PFUser {
                                //-------------------------------------------------------------------------------------//
                                if user.objectId != PFUser.current()?.objectId {

                                    if let email = user.username {
                                        UserDefaults.standard.set(email, forKey: "email")
                                    }

                                    if let coverFile = user["coverPhoto"] {
                                        UserDefaults.standard.set(coverFile, forKey: "coverPhoto")
                                    }

                                    if let profileFile = user["profilePicture"] {
                                        UserDefaults.standard.set(profileFile, forKey: "profilePicture")
                                    }

                                    if let firstName = user["firstName"] {
                                        UserDefaults.standard.set(firstName, forKey: "firstName")
                                    }

                                    if let middleName = user["middleName"] {
                                        UserDefaults.standard.set(middleName, forKey: "middleName")
                                    }

                                    if let lastName = user["lastName"] {
                                        UserDefaults.standard.set(lastName, forKey: "lastName")
                                    }

                                    if let mobileNumber = user["mobileNumber"] {
                                        UserDefaults.standard.set(mobileNumber, forKey: "mobileNumber")
                                    }

                                    if let birthday = user["BirthDate"] {
                                        UserDefaults.standard.set(birthday, forKey: "BirthDate")
                                    }

                                    if let gender = user["gender"] {
                                        UserDefaults.standard.set(gender, forKey: "gender")
                                    }

                                    if let aboutYou = user["about"] {
                                        UserDefaults.standard.set(aboutYou, forKey: "about")
                                    }

                                    if let username = user["usersUsername"] {
                                        UserDefaults.standard.set(username, forKey: "username")
                                    }

                                    gloFirstName = UserDefaults.standard.object(forKey: "firstName") as! String
                                    gloMiddleName = UserDefaults.standard.object(forKey: "middleName") as! String
                                    gloLastName = UserDefaults.standard.object(forKey: "lastName") as! String
                                    gloMobNumber = UserDefaults.standard.object(forKey: "mobileNumber") as! String
                                    gloEmail = UserDefaults.standard.object(forKey: "email") as! String
                                    gloUName = UserDefaults.standard.object(forKey: "username") as! String
                                    gloBirthDate = UserDefaults.standard.object(forKey: "BirthDate") as! String
                                    gloGenderStr = UserDefaults.standard.object(forKey: "gender") as! String
                                    gloAbout = UserDefaults.standard.object(forKey: "about") as! String

                                    if let coverImageData = UserDefaults.standard.object(forKey: "coverPhoto"),
                                        let coverPhoto = UIImage(data: coverImageData as! Data){

                                        gloCoverImage = coverPhoto

                                    }


                                    if let profileImageData = UserDefaults.standard.object(forKey: "profilePicture"),
                                        let profilePicture = UIImage(data: profileImageData as! Data){
                                        gloProfilePicture = profilePicture

                                    }


                                }//-----------------------------------------------------------------------------//

                            }

                        }

                    }

                })

没关系,解决了。这个

if user.objectId != PFUser.current()?.objectId {

应该是这个,

if user.objectId == PFUser.current()?.objectId {

因为我需要显示我当前登录的帐户详细信息。

也无法将图像保存到 UserDefaults。因此,每次打开应用程序时只需加载它并将其存储在 var myVariableName = UIImage() 中,连同一些 if 语句检查,您可以强制将其解包为:var myVariableName = UIImage()!

谢谢!