-1

解析とすべてをセットアップしました。サインアップは必要に応じて完全に機能します。しかしその後、オフラインで使用するためにユーザーの詳細をデバイスに戻すことができません。

ユーザーがログイン/サインアップしたら、これらのオブジェクトをオフラインにする必要があります。

クラス名 :User

オブジェクト:

  1. coverPhotoImage.png(ファイル保存)

  2. profilePictureImage.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