-1

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

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

班级名称 :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")但没有用。:\

这是我反复得到的。 1]

这就是我写的:

@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

                                    }


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

                            }

                        }

                    }

                })
4

1 回答 1

0

没关系,解决了。这个

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

应该是这个

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

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

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

不管怎么说,还是要谢谢你!

于 2016-12-18T15:32:30.547 回答