OGeek|极客世界-中国程序员成长平台

标题: ios - Segue 可选值 nil [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:43
标题: ios - Segue 可选值 nil

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let newuser = users[indexPath.row]
    let usernname = newuser.name! as String
    print(usernname)
    print(keys[indexPath.row])
    let destinationViewController = ChatViewController()
    destinationViewController.toUid = keys[indexPath.row]
    destinationViewController.topBar.title = usernname
    performSegue(withIdentifier: "toChatView", sender: self)
}

keys[indexPath.row] 工作正常,但用户名有错误。它说

fatal error: unexpectedly found nil while unwrapping an Optional value

但是当我打印我的 username 变量时,它会打印出来



Best Answer-推荐答案


你需要检查用户名是否为 nil

let usernname = newuser.name! as String

let destinationViewController = ChatViewController()
destinationViewController.toUid = keys[indexPath.row]

if username != nil { // check if this is nil or not
 print(usernname)
 print(keys[indexPath.row])
 destinationViewController.topBar.title = usernname
 performSegue(withIdentifier: "toChatView", sender: self)
}

关于ios - Segue 可选值 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39848319/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4