如果出现问题,我试图检查何时将对象保存到我的解析服务中。但我有两个选择,我有三个选择,我不知道其中的区别。我有这三个选项(如果error == nil,或者如果object!= nil,或者如果error == nil and object!= nil)。我应该使用哪一个。感谢
选项#1
let user = PFUser.current()!
user.saveInBackground (block: { (success:Bool, error:Error?) -> Void in
if error == nil{
}
)}
选项#2
let user = PFUser.current()!
user.saveInBackground (block: { (success:Bool, error:Error?) -> Void in
if object != nil{
}
)}
选项#3
let user = PFUser.current()!
user.saveInBackground (block: { (success:Bool, error:Error?) -> Void in
if error == nil && object != nil{
}
)}
答案 0 :(得分:0)
最好使用不同的处理程序来获得成功和失败:
func saveInBackground(success: () -> Void, failure: (Error?) -> Void) {
/* do whatever you need */
if saved {
success()
} else {
failure(saveError)
}
}
saveInBackground(success: {
/* saving was succeed */
}, failure: { (error) in
/* saving was failed */
})
答案 1 :(得分:0)
保存操作成功时不应该出现错误,一般来说,这取决于您实施的后续步骤。没有一种方法是无效的,我仍然建议您在检查错误之前检查'happy value',object,success dtc ...
答案 2 :(得分:0)
我建议并鼓励你使用保护声明,这是针对这种情况所做的。
//Always safely unwrap optional value:
if let user = PFUser.current(){
user.saveInBackground (block: { (success:Bool, error:Error?) -> Void in
guard success, error == nil else {
//handle error somehow...(print or whatever...)
return
}
//Continue here as everything is fine...
)}
}