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-推荐答案 strong>
你需要检查用户名是否为 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/
|