更新信息用户个人资料Swift 4

时间:2018-08-17 08:51:08

标签: ios json swift token

您好,我只是实现了在swift 4上修改和更新用户个人资料的方法,并且在更新令牌中的用户信息(名字和姓氏)时遇到了问题

 //Get token
let token = HPWSLoginManager.shared().saveSuccessResponse.token

// Bearer token to update information
let url = URL(string: "http://51.38.36.76:40/api/v1/updateProfile")
var request = URLRequest(url: url!)
request.httpMethod = "PUT"
request.addValue("Bearer \(token!)", forHTTPHeaderField: "Authorization")

//serialization token
URLSession.shared.dataTask(with: request) { (data, response, error) in
    guard let data = data else { return }
    do {
        let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: AnyObject]
        let sub = json["sub"] as? [String: AnyObject]

         DispatchQueue.main.async {
        //Get current user
        let myUser = PFUser.current()

        // check if firstName, lastName are not empty
        if(self.firstNameTextfield.text!.isEmpty || self.lastNameTextfield.text!.isEmpty )
        {
            let myAlert = UIAlertController(title: "Alert", message: "First name and Last name are required fields", preferredStyle: UIAlertControllerStyle.alert);
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
            myAlert.addAction(okAction);
            self.present(myAlert, animated: true, completion: nil);
            return
        }

        // set new values user
        let userFirstName = self.firstNameTextfield.text
        let userLastName = self.lastNameTextfield.text

        // update information    
        myUser?.setObject(userFirstName, forKey: "\(sub?["firstname"])")
        myUser?.setObject(userLastName, forKey: "\(sub?["lastname"])")

        //display activity indicator
        let loadingNotification = MBProgressHUD.showAdded(to: self.view, animated: true)
            loadingNotification.labelText = "sauvegarde des informations"
            myUser?.saveInBackground(block: { (success:Bool, error:NSError?) -> Void in

            // Hide activity indicator
            loadingNotification.hide(animated: true)

            if(error != nil)
            {
                let myAlert = UIAlertController(title: "Alert", message: error!.localizedDescription, preferredStyle: UIAlertControllerStyle.alert);
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
                myAlert.addAction(okAction);
                self.present(myAlert, animated: true, completion: nil);
                return
            }
            if(success)
            {
                let userMessage = "votre profil a été mis a jour"
                let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.alert);
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction!) -> Void in
                    self.dismiss(animated: true, completion: {() -> Void in
                        //                            self.opener.loadUserDetails()
                    })
                })
                myAlert.addAction(okAction);
                self.present(myAlert, animated: true, completion: nil);
            }
                } as! PFBooleanResultBlock)
       }

    } catch {
        print("error")
    }
    }.resume()

我不知道这是否是更新信息的正确方法,当我抓住时,他恢复了 userFirstName userLastName ,但是myUser?.setObject(userFirstName, forKey: "\(sub?["firstname"])")在屏幕上显示nil安慰。我真的需要您的帮助,在此先感谢您:)

1 个答案:

答案 0 :(得分:0)

尝试替换以上代码中的这些行-

Model.Init
相关问题