【问题标题】:Error Checking does not work properly for Parse Query错误检查不适用于 Parse Query
【发布时间】:2026-02-22 19:25:01
【问题描述】:

所以我在解析服务器中有一个名为“类”的类,我试图在我的程序中实现一个错误检查查询。 Xcode 项目可以正确构建,但是,当我尝试错误检查查询以搜索类时,程序总是说搜索成功,即使该类类型的对象不在查询中。代码如下:

  @IBAction func searchClass(_ sender: Any) {

    let query = PFQuery(className: "Classes")
    query.findObjectsInBackground { (success, error) in
        if error != nil{
            self.createAlert(title: "Error", message: "Cannot find class")
        } else {
            self.createAlert(
                title: "Success", message: "Found the query")
            self.classSuccess = true
            self.flipCardButton.alpha = 0
            UIView.transition(from: self.classQuery, to: self.classRate, duration: 0.5, options: .transitionFlipFromRight)
            self.goBackButton.alpha = 1
        }
    }

这是我的应用程序中的搜索按钮:

Screenshot of Simulator

解析查询检查总是打印成功可能会出什么问题?

【问题讨论】:

    标签: ios swift xcode parse-platform


    【解决方案1】:

    我认为您在查询中缺少成功声明。

    let query = PFQuery(className: "Classes")
    query.findObjectsInBackground { (success, error) in
        if error != nil{
            print(error ?? "") 
            self.createAlert(title: "Error", message: "Cannot find class")
        } 
    
        if success {
            self.createAlert(
                title: "Success", message: "Found the query")
            self.classSuccess = true
            self.flipCardButton.alpha = 0
            UIView.transition(from: self.classQuery, to: self.classRate, duration: 0.5, options: .transitionFlipFromRight)
            self.goBackButton.alpha = 1
    
        } else {
           self.createAlert(title: "Error", message: "Cannot find class")
        }
    }
    

    【讨论】: