致命错误:在 Swift 中解开 Optional 值时意外发现 nil [英] fatal error: unexpectedly found nil while unwrapping an Optional value in Swift

查看:35
本文介绍了致命错误:在 Swift 中解开 Optional 值时意外发现 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在 Swift 中获取输入字段的文本.所以这就是我得到的

So I'm trying to get the text of an input field in Swift. So here's what I got

class ViewController: UIViewController {

    @IBOutlet var passwordField: UITextField!
    @IBOutlet var usernameField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()            
        //Try to log user in by default
        let user = usernameField.text
        let password = passwordField.text

一切看起来都不错(我知道我没有关闭类,我只是直接将其拉出)并且当我去运行它时,我得到

It all looks good (I know I didn't close the class, I just pulled it directly out) and when I go to run it, I get

致命错误:在展开可选值时意外发现 nil(lldb)

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

(您必须在新标签页中打开图片才能看到)

(You'll have to open the image in a new tab to see it)

我成功地将输入字段与 IBOutlet 绑定,我知道.有任何想法吗?

I binded the input fields with the IBOutlet successful, I know that. Any ideas?

推荐答案

尝试这种方式我认为这会奏效 :

Try this way I think this will work :

if let abc = usernameField.text{
        let user = abc

    }

这篇关于致命错误:在 Swift 中解开 Optional 值时意外发现 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆