首页 文章

Swift 3重做代码在没有If的情况下离开(Parse Call)(编辑)

提问于
浏览
-3

我的任务是将此应用程序的解析调用合并到一个文件中 . 许多调用都附加了某种UI任务 . 因此实际调用在文件中,而UI任务仍在其适当的视图控制器中 . 我遇到的一个问题是一个if let语句,其中一个else提示了一个警告,但if let必须保留在APIManager文件中 . 所以,基本上 if let objects = objects as [PFObject]? 必须保持原样,但视图控制器上的最后一个else语句最初附加到它上面所以它现在没有任何东西可以实现 .

为了澄清,第一个和第二个代码片段只是第三个片段分隔,然后查询放入一个单独的文件中的不同函数,并在函数中添加了一个完成处理程序 . 执行此操作时,代码的 if let objects = objects as [PFObject]? 部分必须保留在查询中,但第二个else语句依赖于它 . 因为如果没有对象,则显示错误 . 我正在试图弄清楚如何保持这个功能 .

下面是解析调用,然后是视图控制器中随附的代码 . 我还将在此下面包含原始方法 .

func viewNeedMetButton(completion: @escaping([PFObject?]) -> Void) {
    let query = PFQuery(className: "Need")
    query.whereKey("committed", equalTo: true)
    query.findObjectsInBackground {(objects: [PFObject]?, error: Error?) in
        if let objects = objects as [PFObject]? {
            completion(objects)
        }
    }
}

视图控制器上与解析调用一起使用的代码 .

@IBAction func markAsMet(_ sender: Any) {
    viewNeedMetButton { (objects) in
        if NeedStore.shared.currentNeed?.committed == true {
            for object in objects {
                NeedStore.shared.needObject?.setObject(true, forKey: "met")
                NeedStore.shared.needObject?.saveInBackground() { (success, error) -> Void in
                    if success {
                        let alert = UIAlertController(title: "Success", message: "You have marked this need as met!", preferredStyle: .alert)
                        let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {
                            (_)in
                            self.performSegue(withIdentifier: "unwindToNeedsList", sender: self)
                        })
                        alert.addAction(OKAction)
                        self.present(alert, animated: true, completion: nil)
                    }
                }
            }
        } else {
            let alert = UIAlertController(title: "Error", message: "You cannot declare a need as met if it has not been committed to.", preferredStyle: .alert)
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
            alert.addAction(okAction)
            self.present(alert, animated: true, completion: {
                return
            })
        } else {
            let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
            alert.addAction(okAction)
            self.present(alert, animated: true, completion: {
                return
            })
        }
    }
}

最后是原始代码,其中所有内容都在同一个函数中 .

@IBAction func markAsMet(_ sender: Any) {
    let query = PFQuery(className: "Need")
    query.whereKey("committed", equalTo: true)
    query.findObjectsInBackground {(objects: [PFObject]?, error: Error?) in
        if let objects = objects as [PFObject]? {
            if NeedStore.shared.currentNeed?.committed == true {
                for object in objects {
                    NeedStore.shared.needObject?.setObject(true, forKey: "met")
                    NeedStore.shared.needObject?.saveInBackground() { (success, error) -> Void in
                        if success {
                            let alert = UIAlertController(title: "Success", message: "You have marked this need as met!", preferredStyle: .alert)
                            let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {
                                (_)in
                                self.performSegue(withIdentifier: "unwindToNeedsList", sender: self)
                            })
                            alert.addAction(OKAction)
                            self.present(alert, animated: true, completion: nil)
                        }
                    }
                }
            } else {
                let alert = UIAlertController(title: "Error", message: "You cannot declare a need as met if it has not been committed to.", preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
                alert.addAction(okAction)
                self.present(alert, animated: true, completion: {
                    return
                })
            }
        } else {
            let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
            alert.addAction(okAction)
            self.present(alert, animated: true, completion: {
                return
            })
        }
    }
}

我非常感谢任何帮助或建议:)

1 回答

  • 0

    正如其他人在评论中所写:如果有2个其他不起作用 .

    我不知道为什么你需要将 if 移动到另一个功能 . 但在这里我的解决方案如何让它像以前一样工作

    hint: 如果你想将一个零件提取到另一个零件,那么你必须在相同的水平上切割它:在你的情况下从 if NeedStore.shared.currentNeed?.committed == true { 直到 } 在同一水平的if上(这是一个级别上的一个块: if condition { ... } else { ... } ) . 当你切断这个 1. Solution 的剩余部分切割的地方不在线 completion(objects)

    1.解决方案

    你需要将else部分也移动到另一个函数 viewNeedMetButton ,因为它在错误时被调用 .

    func viewNeedMetButton(completion: @escaping([PFObject?]) -> Void) {
        let query = PFQuery(className: "Need")
        query.whereKey("committed", equalTo: true)
        query.findObjectsInBackground {(objects: [PFObject]?, error: Error?) in
            if let objects = objects as [PFObject]? {
                completion(objects)
            } else {
                let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
                alert.addAction(okAction)
                self.present(alert, animated: true, completion: {
                    return
                })
            }
        }
    }
    

    视图控制器上与解析调用一起使用的代码 .

    @IBAction func markAsMet(_ sender: Any) {
        viewNeedMetButton { (objects) in
            if NeedStore.shared.currentNeed?.committed == true {
                for object in objects {
                    NeedStore.shared.needObject?.setObject(true, forKey: "met")
                    NeedStore.shared.needObject?.saveInBackground() { (success, error) -> Void in
                        if success {
                            let alert = UIAlertController(title: "Success", message: "You have marked this need as met!", preferredStyle: .alert)
                            let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {
                                (_)in
                                self.performSegue(withIdentifier: "unwindToNeedsList", sender: self)
                            })
                            alert.addAction(OKAction)
                            self.present(alert, animated: true, completion: nil)
                        }
                    }
                }
            } else {
                let alert = UIAlertController(title: "Error", message: "You cannot declare a need as met if it has not been committed to.", preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
                alert.addAction(okAction)
                self.present(alert, animated: true, completion: {
                    return
                })
            } 
        }
    }
    

    2.解决方案

    如果你想要调用 viewNeedMetButton 更通用,那么添加一个 onError 闭包 .

    func viewNeedMetButton(completion: @escaping([PFObject?]) -> Void,
                              onError: @escaping(Error?) -> Void) {
        let query = PFQuery(className: "Need")
        query.whereKey("committed", equalTo: true)
        query.findObjectsInBackground {(objects: [PFObject]?, error: Error?) in
            if let objects = objects as [PFObject]? {
                completion(objects)
            } else {
                onError(error)
            }
        }
    }
    

    视图控制器上与解析调用一起使用的代码 .

    @IBAction func markAsMet(_ sender: Any) {
        viewNeedMetButton(completion: { (objects) in
            if NeedStore.shared.currentNeed?.committed == true {
                for object in objects {
                    NeedStore.shared.needObject?.setObject(true, forKey: "met")
                    NeedStore.shared.needObject?.saveInBackground() { (success, error) -> Void in
                        if success {
                            let alert = UIAlertController(title: "Success", message: "You have marked this need as met!", preferredStyle: .alert)
                            let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {
                                (_)in
                                self.performSegue(withIdentifier: "unwindToNeedsList", sender: self)
                            })
                            alert.addAction(OKAction)
                            self.present(alert, animated: true, completion: nil)
                        }
                    }
                }
            } else {
                let alert = UIAlertController(title: "Error", message: "You cannot declare a need as met if it has not been committed to.", preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
                alert.addAction(okAction)
                self.present(alert, animated: true, completion: {
                    return
                })
            } 
        },
        onError: {
             (error?) in
    
                let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
                alert.addAction(okAction)
                self.present(alert, animated: true, completion: {
                    return
                })
        })
    }
    

相关问题