Paal Aune Paal Aune - 1 year ago 69
iOS Question

How to locate the line? unexpectedly found nil

When I try to run my app, I get the error stating:


fatal error: unexpectedly found nil while unwrapping an Optional
value.


Can anybody tell me if its a way to locate the line where the problem is?
Unfortunately, I don't get a red line where the simulator crashes.

I pasted in all of the code, but the problem must have to do with the alert function because it worked fine until I tried to implement that.

import UIKit

var list = ["Visa code: 1234", "Mastercard code: 4321"]

class notesVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var messageLabel: UILabel!

var userMessage = "Sample text"
var theUserText: UITextField?

@IBOutlet weak var tabelView: UITableView!
@IBAction func addItemButton(_ sender: Any) {
let alertController = UIAlertController(title:"title",
message: "message",
preferredStyle: .alert)

alertController.addTextField(configurationHandler: theUserTextFunc)
let okAction = UIAlertAction(title: "OK",
style: .default,
handler: self.okHandler)

let cancleAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancleAction)
self.present(alertController, animated: true)
}

func theUserTextFunc(textField: UITextField){
theUserText = textField
}

func okHandler(alert: UIAlertAction!){
list.append((theUserText?.text)!)
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return (list.count)
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")

cell.textLabel?.text = list[indexPath.row]
return(cell)
}


// Swipe to delete an item
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete{
list.remove(at: indexPath.row)
tabelView.reloadData()
}
}

override func viewDidLoad() {
super.viewDidLoad()
messageLabel.text = userMessage
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func customInit(userMessage: String) {
self.userMessage = userMessage
}
}

Answer Source

I tried your code. It works flawless except that you missed to reload on table on OkHandler.

So I suspect issue would be with ur IBOutlet or IBAction connections. Check around that...

import UIKit

var list = ["Visa code: 1234", "Mastercard code: 4321"]


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var messageLabel: UILabel!


var userMessage = "Sample text"
var theUserText: UITextField?


@IBOutlet weak var tabelView: UITableView!

@IBAction func addItemButton(_ sender: Any) {
    let alertController = UIAlertController(title:"title",
                                            message: "message",
                                            preferredStyle: .alert)

    alertController.addTextField(configurationHandler: theUserTextFunc)
    let okAction = UIAlertAction(title: "OK",
                                 style: .default,
                                 handler: self.okHandler)

    let cancleAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    alertController.addAction(okAction)
    alertController.addAction(cancleAction)
    self.present(alertController, animated: true)

}
func theUserTextFunc(textField: UITextField){
    theUserText = textField

}
func okHandler(alert: UIAlertAction!){

    list.append((theUserText?.text)!)
    self.tabelView.reloadData()
}


public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return (list.count)
}



public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")

    cell.textLabel?.text = list[indexPath.row]

    return(cell)
}


// Swipe to delete an item
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == UITableViewCellEditingStyle.delete{
        list.remove(at: indexPath.row)
        tabelView.reloadData()




    }
}


override func viewDidLoad() {
    super.viewDidLoad()
    messageLabel.text = userMessage


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}
func customInit(userMessage: String) {
    self.userMessage = userMessage
}

}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download