RNK
RNK

Reputation: 5792

unexpectedly found nil in swift

I have code like this in my controller:

var itemList:[String] = []

@IBOutlet var tasksTable:UITableView!

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

    cell.textLabel.text = itemList[indexPath.row]

    return cell
}

override func viewWillAppear(animated: Bool) {
    tasksTable.reloadData() <-----------------------
}

Why am I getting this error?

ERROR: unexpectedly found nil while unwrapping an Optional value

Upvotes: 0

Views: 175

Answers (1)

matt
matt

Reputation: 535999

It is because at the moment viewWillAppear is called, tasksTable is nil. You probably forgot to hook up the outlet in the nib.

Upvotes: 3

Related Questions