迅速-检索objectId字段导致致命错误 [英] swift - retrieve objectId field causes fatal error
问题描述
这是我的代码:
@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代码.我已将其设置为在新用户注册该应用程序时运行.我已验证新行已插入到解析云数据库中的设置表中.但是我现在想简单地更新单行,而不是每次保存时都创建一个新行.因此,我一直在进行updateSettings函数.我已经按照Parse文档中的说明更新了对象( https://parse.com/docs/ios_guide #objects-updating/iOS ).当我单击更新"按钮以运行该功能时,我得到:
I can run the saveSettings code with no issues. I have set it up to run when a new user signs up with the application. I have verified that a new row is inserted into the settings table in the Parse cloud DB. But I would now like to simply UPDATE the single row instead of create a new row every time there is a save. So I have been working on the updateSettings function. I have followed the Parse documentation for updating objects (https://parse.com/docs/ios_guide#objects-updating/iOS). When I click on the Update button to run the function, I get:
严重错误:解开可选值时意外发现nil (lldb)
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
它指向的行是:
var id = settings["objectId"] as String
我是Parse的完整入门者.
I am a complete beginner to Parse.
一些最近的附加信息:
println语句的结果:
The results of the println statements:
<Settings: 0x7fd423805260,
objectId: new, localId: (null)> {
}
nil
<PFQuery: 0x7fd4217191f0>
那么问题的一部分就是它不是在检索objectId吗?
So part of the problem is it is not retrieving the objectId?
推荐答案
PFObject
具有称为"objectId"的属性,它没有存储在字典中,因此您可以将其作为
PFObject
has a property called "objectId", it is not stored in a dictionary, so you can return it as
var id = settings.objectId
这篇关于迅速-检索objectId字段导致致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!