致命错误:解开可选值时意外发现nil-为什么? [英] fatal error: unexpectedly found nil while unwrapping an Optional value - why?

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

问题描述

我对使用Swift进行编码非常陌生,我不太确定这里发生了什么-有人可以帮忙吗?

I'm pretty new to coding in Swift and I'm not too sure what's happening here - can anyone help?

谢谢

import UIKit

class SecondViewController: UIViewController {

    var toDoItems:[String] = []


    @IBOutlet weak var toDoItem: UITextField!

    @IBAction func addItem(sender: AnyObject) {

        toDoItems.append(toDoItem.text)
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

        println(toDoItems)



    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

推荐答案

您的IBOutlettoDoItem很可能没有在Interface Builder中成功绑定到UITextField.在Interface Builder中检查文本字段的出口,并确保其已成功连接.

Most likely, your IBOutlet, toDoItem, was not bound successfully to a UITextField in Interface Builder. Check the outlets for your text field in Interface Builder and make sure it's hooked up successfully.

如果插座正确连接,则此问题的另一个候选来源是视图控制器本身的实例化.如果以编程方式实例化它(例如SecondViewController()而不是storyboard.instantiateViewControllerWithIdentifier(...)),那也会导致此错误.

If the outlet is hooked up properly, another candidate source of this problem is the instantiation of the view controller itself. If you instantiated it programmatically (e.g. SecondViewController() instead of storyboard.instantiateViewControllerWithIdentifier(...)), that would also result in this error.

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

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