gpt4 book ai didi

ios - 使用 Parse 在 iOS Swift 中更新对象

转载 作者:行者123 更新时间:2023-11-28 07:06:06 26 4
gpt4 key购买 nike

我有一个“首选项”表,其中用户首选项与用户名一起保存。我创建了这个方法来更新当前用户的偏好'但不知何故它似乎不起作用。我不确定是否需要“prefQuery.getObjectInBackgroundWithId(object.objectId)”部分。

我是 Parse 的新手,有人可以帮我指出可能是什么问题吗。

func userPreferences(){
var currUser = PFUser.currentUser()
var prefQuery = PFQuery(className: "preferences")
var prefObj = PFObject(className: "preferences")

if let currUserName = PFUser.currentUser()?.username {
prefQuery.whereKey("username", equalTo: currUserName)
}

prefQuery.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in

if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {

prefQuery.getObjectInBackgroundWithId(object.objectId){
(object: PFObject?, error: NSError?) -> Void in
if error == nil || object != nil {
prefObj["agestart"] = self.fromAge.text
prefObj["ageend"] = self.toAge.text
prefObj["location"] = self.location.text
ProgressHUD.showSuccess("Update successful")
} else {
ProgressHUD.showError("Update failed")
}
}
}
}
}
}
}

最佳答案

我发现了问题并更新了我的代码。工作代码如下;问题出在“prefObj["agestart"]”代码块上,我在其中使用了错误的查询实例。您可以比较这两个片段:

func userPreferences(){
var currUser = PFUser.currentUser()
var prefQuery = PFQuery(className: "preferences")
var prefObj = PFObject(className: "preferences")

if let currUserName = PFUser.currentUser()?.username {
prefQuery.whereKey("username", equalTo: currUserName)
}

prefQuery.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in

if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
prefQuery.getObjectInBackgroundWithId(object.objectId){
(prefObj: PFObject?, error: NSError?) -> Void in
if error != nil {
println(error)
ProgressHUD.showSuccess("Error while updating")
} else if let prefObj = prefObj {
prefObj["agestart"] = self.fromAge.text
prefObj["ageend"] = self.toAge.text
prefObj["location"] = self.location.text
ProgressHUD.showSuccess("Update successful")
prefObj.saveInBackgroundWithBlock({ (Bool, error: NSError!) -> Void in })
}
}
}
}
}
}
}

关于ios - 使用 Parse 在 iOS Swift 中更新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30592190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com