ios - 我的 IF 语句只会运行一个条件 - iOS Swift

标签 ios swift if-statement parse-platform

我正在尝试将一个新对象保存到我的解析数据库中,基本上,如果用户正在上传文档并且满足特定条件的文档已经存在,我想删除旧对象并使用新对象进行更新。这是我的代码:

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/

相关文章:

ios - 为什么点击手势识别器会识别其父 View 中的点击?

swift - 具有多个输入和多个输出的游戏 AI 的 CoreML

ios - UISearchController 不会关闭 iOS 8 Swift 的搜索栏和重叠

c - If 链表中的语句未按预期工作

java - 如何重构代码以删除 if 语句?

list - 序言; if 和(停止)递归

ios - 为 ios 8.1 设计的应用程序会自动在 ios 8.2 上运行吗?

ios - swift 在应用组容器中创建目录

原型(prototype)单元格内按钮上的 iOS UIButton 标题文本

ios - 从 UIPastboard 粘贴字符串数据