如何在 "return data" 之后添加函数
How to add function after the "return data"
我有一个问题,我的程序可以 运行 但某些代码无法运行。没有错误,但它有一个警告标志说“return 之后的代码将不会执行”。我想在这方面提供帮助。谢谢
@IBAction func newBookTapped(_ sender: Any) {
guard let uid = Auth.auth().currentUser?.uid,
let data = bookData() else {
return
}
db.collection("new reading").document(uid).setData(data)
}
func bookData() -> [String: Any]? {
guard let title = bookTitleTextField.text,
let author = bookAuthorTextField.text,
let summary = bookSummaryTextField.text else {
return nil
}
let data: [String: Any] = [
"bookTitle": title,
"bookAuthor": author,
"bookSummary": summary
]
return data
self.transitionToMenu()
}
func transitionToMenu() {
let alert = UIAlertController(title: nil, message: "Book has been saved!", preferredStyle: .alert)
alert.view.alpha = 0.5
alert.view.layer.cornerRadius = 15
self.present(alert, animated: true)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
if let navController = self.navigationController {
navController.popViewController(animated: true)
} else {
self.dismiss(animated: true, completion: {})
}
}
}
My program photo
在创建 return 时,您结束函数并返回调用,因此无法执行函数中 return 之后的任何内容。
您必须将 self.transitionToMenu()
放在 return data
之前。
我有一个问题,我的程序可以 运行 但某些代码无法运行。没有错误,但它有一个警告标志说“return 之后的代码将不会执行”。我想在这方面提供帮助。谢谢
@IBAction func newBookTapped(_ sender: Any) {
guard let uid = Auth.auth().currentUser?.uid,
let data = bookData() else {
return
}
db.collection("new reading").document(uid).setData(data)
}
func bookData() -> [String: Any]? {
guard let title = bookTitleTextField.text,
let author = bookAuthorTextField.text,
let summary = bookSummaryTextField.text else {
return nil
}
let data: [String: Any] = [
"bookTitle": title,
"bookAuthor": author,
"bookSummary": summary
]
return data
self.transitionToMenu()
}
func transitionToMenu() {
let alert = UIAlertController(title: nil, message: "Book has been saved!", preferredStyle: .alert)
alert.view.alpha = 0.5
alert.view.layer.cornerRadius = 15
self.present(alert, animated: true)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
if let navController = self.navigationController {
navController.popViewController(animated: true)
} else {
self.dismiss(animated: true, completion: {})
}
}
}
My program photo
在创建 return 时,您结束函数并返回调用,因此无法执行函数中 return 之后的任何内容。
您必须将 self.transitionToMenu()
放在 return data
之前。