因此,当我尝试在Swift中使用present()函数时,它总是崩溃。这是我的代码
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错误。有人可以帮我吗?
最佳答案
使用if let
或guard let
代替用!
强制展开Optionals可以使您的代码更健壮:
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)
}
关于ios - present(_:animated:completion :)在Swift 3中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42412191/