存在(_:动画:完成:)不是斯威夫特3个工作

问题描述:

所以,当我尝试使用斯威夫特本()函数,它总是崩溃。这里是我的代码存在(_:动画:完成:)不是斯威夫特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值正确与否,因为代码是正确的 –

使用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) 

} 

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 ..所以你可以请检查自己的范围... –

你说你的错误是:“致命错误:意外发现零而展开的可选值“ 该问题可能不是present()方法,而是一些IBOutlet或您的ViewController上的变量。 检查所有网点是否连接正确,或者如果这些变量不接受nil,那么你在“username”和“imageURL”上设置的内容不为零。