Home > Mobile >  How to add function after the "return data"
How to add function after the "return data"

Time:12-08

I have a problem where my program could run but some of the code didn't work. There was no error but it has a warning sign says that the "code after return will not executed". I would like a help on that. Thank you

@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

CodePudding user response:

When making a return you end the function and go back to the call so there is no way that anything after a return in a function is being executed.

You have to put the self.transitionToMenu() before the return data.

  • Related