我正在尝试将一个新对象保存到我的解析数据库中,基本上,如果用户正在上传文档并且满足特定条件的文档已经存在,我想删除旧对象并使用新对象进行更新。这是我的代码:
if object.objectId == nil {
postImage.saveInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if let error = error {
if let errorString = error.userInfo["error"] as? String {
self.displayAlert("Please choose an image and expiration date", message: errorString)
}
} else {
print("Saved new object \(postImage)")
print("Saved new object:\(self.expirationField.text)")
self.dismissViewControllerAnimated(true, completion: nil)
self.expirationField.text = ""
self.imageToPost.image = UIImage(named: "addDocument.jpg")
}
}
} else {
object.deleteInBackground()
postImage.saveInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if let error = error {
if let errorString = error.userInfo["error"] as? String {
self.displayAlert("Please choose an image and expiration date", message: errorString)
}
} else {
print("Saved new object \(postImage)")
print("Deleted old object and added:\(self.expirationField.text)")
self.dismissViewControllerAnimated(true, completion: nil)
self.expirationField.text = ""
self.imageToPost.image = UIImage(named: "addDocument.jpg")
}
}
}
所以最终发生的情况是它永远不会运行第一个条件,但应用程序可以正常编译。如果在模拟器中它符合第一个条件,它就只有我的事件指示器永远运行并且不会保存以进行解析。非常感谢任何帮助!!
最佳答案
if let error = error
在这里,您正在设置变量的值,并且无论变量是什么,即使来自 Parse 的错误值为 nil/null,该值都是 true。
关于ios - 我的 IF 语句只会运行一个条件 - iOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33879904/