ios - present(_:animated:completion :)在Swift 3中不起作用

标签 ios swift3

因此,当我尝试在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 letguard 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/

相关文章:

ios - 图像是否会在 iOS 更新时从本地文件系统中删除?

ios - 快速保存和检索 UserDefaults 中的自定义对象

ios - 什么是 CGFloat.random(min : CGFloat, max : CGFloat) of swift 2 in swift 3?

ios - 通过 UIImagePickerController 使用相机在 iPod 上崩溃

objective-c - iOS SDK,Objective-C,仅可显示一个单元格

ios - 带有 HTTP 身份验证的 NSURLRequest

ios - 使用CKCloud套件共享私有(private)记录

swift - 在多种方法中使用相同的货币格式

swift - 如何更改标签中某些单词的颜色 - Swift 3

iPhone : Upload video to Youtube while running app in device