这是我的代码:
@IBAction func saveSettings(sender: AnyObject) {
var settings:PFObject = PFObject(className: "Settings")
settings["routes"] = routesSetting as String
settings["sortBy"] = sortBySetting as String
settings["user"] = PFUser.currentUser()
settings.saveInBackgroundWithBlock{(success:Bool!, error:NSError!) ->Void in
if success != nil {
NSLog("%@","OK-settings data saved")
NSLog("%@",self.routesSetting as String)
}
else
{
NSLog("%@",error)
}
}
}
@IBAction func updateSettings(sender: AnyObject) {
var settings:PFObject = PFObject(className:"Settings")
var id = settings["objectId"] as String
var query = PFQuery(className:"Settings")
println(settings)
println(id)
println(query)
query.getObjectInBackgroundWithId(id) {
(settings: PFObject!, error: NSError!) -> Void in
if error != nil {
NSLog("%@", error)
} else {
settings["routes"] = self.routesSetting as String
settings["sortBy"] = self.sortBySetting as String
settings.save()
}
}
我可以毫无问题地运行saveSettings代码。我已将其设置为在新用户注册应用程序时运行。我已经验证了在Parse云DB中的设置表中插入了一个新行。但我现在想简单地更新单行,而不是每次保存时都创建一个新行。所以我一直在研究updateSettings函数。我已经按照Parse文档来更新对象(https://parse.com/docs/ios_guide#objects-updating/iOS)。当我点击“更新”按钮运行该功能时,我得到:
致命错误:在展开Optional值时意外发现nil (lldb)
它所指的是:
var id = settings["objectId"] as String
我是Parse的初学者。
最近的一些补充信息:
println语句的结果:
<Settings: 0x7fd423805260,
objectId: new, localId: (null)> {
}
nil
<PFQuery: 0x7fd4217191f0>
问题的一部分是它没有检索objectId?
答案 0 :(得分:2)
PFObject
有一个名为&#34; objectId&#34;的属性,它不存储在字典中,因此您可以将其作为
var id = settings.objectId