gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:41:27 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com