【发布时间】:2018-08-17 08:51:08
【问题描述】:
您好,我只是在 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。我真的需要你的帮助提前谢谢你:)
【问题讨论】:
-
显示 nil 是什么意思,它是一个 set 方法,那么显示 nil 是什么?还有
"\(sub?["firstname"])"是什么意思,看起来你要使用的键是字典中的值? -
myUser?.setObject(userFirstName, forKey: "\(sub?["firstname"])")应该恢复并设置 userFirstName 的值"\(sub?["firstname"])"是在令牌中提取的名字的值,例如Json[firstname] -
这毫无意义,您正在为您的
myUser对象上的从sub?["firstname"]获得的键添加/设置userFirstName的值。你是什么意思“恢复和设置”?而且我仍然不明白“在控制台上显示 nil”是什么? -
userFirstName将检索在键盘上输入的新值,我将在令牌(sub?["firstname"])的信息中解析它们以进行编辑 -
我仍然不明白您的问题,您想使用从两个文本字段中获得的值更新您的
myUser对象的名字和姓氏,这部分我得到了,但我没有得到什么就是失败了。您需要通过澄清您的问题并可能给出示例来更好地解释该部分。