我的任务是将此应用的解析调用合并到一个文件中。许多调用都附加了某种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
})
}
}
}
我非常感谢您对此提出任何帮助或建议:)
答案 0 :(得分:0)
正如其他人在评论中所写:如果其他2不起作用。
我不知道为什么你需要将if
移动到另一个功能。但在这里我的解决方案如何让它像以前一样工作
提示:如果你想将一个零件提取到另一个零件,那么你必须在同一级别上切割它:在你的情况下从if NeedStore.shared.currentNeed?.committed == true {
直到{{1在if的同一级别上(这是一个级别上的一个块:}
)。并且当你将if condition { ... } else { ... }
切割的剩余部分切掉时1. Solution
你需要将else部分移动到另一个函数completion(objects)
,因为它在错误时被调用。
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
})
}
}
}
更通用,请添加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)
}
}
}