Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 3返工代码使Else没有If(解析调用)(已编辑)_Swift_Parse Platform - Fatal编程技术网

Swift 3返工代码使Else没有If(解析调用)(已编辑)

Swift 3返工代码使Else没有If(解析调用)(已编辑),swift,parse-platform,Swift,Parse Platform,我的任务是将这个应用程序的解析调用整合到一个文件中。很多调用都附带了某种UI任务。因此,当UI任务仍在相应的视图控制器中时,实际调用在文件中。我有一个问题,就是一个if-let语句和一个else语句出现了警报,但是if-let必须保留在apimager文件中。因此,基本上,如果让objects=objects作为[PFObject]?必须保持在原来的位置,但是视图控制器上的最后一条else语句最初是附加到它的,因此现在它之前没有任何内容 为了澄清起见,第一个和第二个代码段只是分开的第三个代码段,

我的任务是将这个应用程序的解析调用整合到一个文件中。很多调用都附带了某种UI任务。因此,当UI任务仍在相应的视图控制器中时,实际调用在文件中。我有一个问题,就是一个if-let语句和一个else语句出现了警报,但是if-let必须保留在apimager文件中。因此,基本上,
如果让objects=objects作为[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) {
    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
            })
        } 
    }
}
@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
            })
    })
}
最后是原始代码,其中所有内容都在同一个函数中

    @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
            })
        }
    }
}

我非常感谢您在这方面提供的任何帮助或建议:)

与其他书面评论一样:如果使用2个else,则不起作用

我不知道您为什么需要将
if
移动到另一个函数。但这里我的解决方案是如何让它像以前一样工作

提示:如果您想将一个部分提取到另一个部分,那么您必须始终在同一级别上剪切它:在您的情况下,从
if NeedStore.shared.currentNeed?.committed==true{
直到
}
在if的同一级别上(这是一级别上的一个块:
if条件{…}else{…}
)。当你把这部分剪掉,剩下的部分就是
1。解决方案
非直线切割
完成(对象)

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
            })
        } 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
            })
        } 
    }
}
@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
            })
    })
}
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 { (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) {
    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
            })
        } 
    }
}
@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
            })
    })
}

第二个没有意义。一个
if
不能有两个
else
子句。它没有
if NeedStore.shared.currentNeed?.committed==true
接受第一条else语句<代码>如果让objects=objects作为[PFObject]?取第二个。第二个是没有if-now的,因为if-let必须在包含解析调用的文件中,那么缩进是错误的。我没有时间数一数花括号,看看是不是这样,还是你的花括号也错了。你的工作是发布可以阅读的代码。你不理解,并且认为这是我的错。前两段代码都来自第三段。我不得不将查询从中解析出来,并将其移到其他地方。当我这样做的时候,我必须向函数中添加一个完成处理程序,以便它在继续之前运行它的所有代码。在这样做时,
如果让objects=objects作为[PFObject]?
必须保留查询。现在,第二条else语句没有附加if。这不是花括号。它不是不可读的。我不知道该怎么解决,我确实认为这是你的错。第二个代码片段声称是一个完整的函数
markAsMet
。如果是这样,它将不会编译,因为它包含一个包含两个非法子句的
If
。至于“你不明白”,请不要这样说。说“我没有说清楚”并让自己说清楚。