2017-02-23 76 views
1

所以,當我嘗試使用斯威夫特本()函數,它總是崩潰。這裏是我的代碼存在(_:動畫:完成:)不是斯威夫特3個工作

func infoOpener(sender: UISwipeGestureRecognizer) { 

    let location: CGPoint = sender.location(in: tableView) 

    let cellPath: IndexPath = tableView.indexPathForRow(at: location)! 


    let VC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "userInfo") as! userInfoVC 

    VC.username = "@\(self.user[cellPath.row].username)" 
    VC.imageUrl = self.user[cellPath.row].imagePath 


    self.present(VC, animated: false, completion: nil) 

} 

每當我使用present(_:animated:completion:)功能,我得到一個EXC_BREAKPOINT錯誤。有人可以幫我嗎?

+0

首先確保你的VC的那個對象已正確創建與否。我的意思是檢查對象'VC'是否爲零。 –

+0

設置斷點自我的調用之前和檢查,如果你的VC是零 – Makaille

+0

請檢查您所發送的數據是通過打印,適當和cellPath值正確與否,因爲代碼是正確的 –

回答

3

使用if letguard let代替力展開選配與!,使您的代碼更健壯:

func infoOpener(sender: UISwipeGestureRecognizer) { 

    let location: CGPoint = sender.location(in: tableView) 

    guard let cellPath: IndexPath = tableView.indexPathForRow(at: location) else { 
     print("No index path found") 
     return 
    } 

    guard let VC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "userInfo") as? userInfoVC else { 
     print("View controller could not be instantiated") 
     return 
    } 

    VC.username = "@\(self.user[cellPath.row].username)" 
    VC.imageUrl = self.user[cellPath.row].imagePath 


    self.present(VC, animated: false, completion: nil) 

} 
1
func infoOpener(sender: UISwipeGestureRecognizer) { 

      let location: CGPoint = sender.location(in: tableView) 
      let cellPath: IndexPath = tableView.indexPathForRow(at: location)! 

      if sender.state == .ended /*present in touch end*/ { 
       let VC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "userInfo") as! userInfoVC 
       VC.username = "@\(self.user[cellPath.row].username)" 
       VC.imageUrl = self.user[cellPath.row].imagePath 
       self.present(VC, animated: false, completion: nil) 
      } 
     } 
+0

它再次崩潰。還要別的嗎? –

+0

你聲明cellPath變量? –

+0

其中實際上是你的方法infoOpener ...通常self.present給我們錯誤時,自我,如果不是一個viewcontroller ..所以你可以請檢查自己的範圍... –

0

你說你的錯誤是:「致命錯誤:意外發現零而展開的可選值「 該問題可能不是present()方法,而是一些IBOutlet或您的ViewController上的變量。 檢查所有網點是否連接正確,或者如果這些變量不接受nil,那麼你在「username」和「imageURL」上設置的內容不爲零。