菜鸟教程小白 发表于 2022-12-11 17:43:07

ios - Segue 可选值 nil


                                            <p><pre><code>func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let newuser = users
    let usernname = newuser.name! as String
    print(usernname)
    print(keys)
    let destinationViewController = ChatViewController()
    destinationViewController.toUid = keys
    destinationViewController.topBar.title = usernname
    performSegue(withIdentifier: &#34;toChatView&#34;, sender: self)
}
</code></pre>

<p>keys 工作正常,但用户名有错误。它说</p>

<pre><code>fatal error: unexpectedly found nil while unwrapping an Optional value
</code></pre>

<p>但是当我打印我的 <code>username</code> 变量时,它会打印出来</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你需要检查用户名是否为 nil </p>

<pre><code>let usernname = newuser.name! as String

let destinationViewController = ChatViewController()
destinationViewController.toUid = keys

if username != nil { // check if this is nil or not
print(usernname)
print(keys)
destinationViewController.topBar.title = usernname
performSegue(withIdentifier: &#34;toChatView&#34;, sender: self)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Segue 可选值 nil,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39848319/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39848319/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Segue 可选值 nil