fatal error: unexpectedly found nil while unwrapping an Optional value - why?

对着背影说爱祢 提交于 2019-12-29 09:31:11

问题


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

Thanks

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.
    }


}

回答1:


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.

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.



来源:https://stackoverflow.com/questions/26348996/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-value-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!