0

I want to create a Swift project. I made a tabbarcontroller and it has 4 view controllers.

In one of them I want to use a table to display 5 strings from a plist. But Xcode tells me:

"unexpectedly found nil while unwrapping an Optional value"

and my tableview have no value.

import UIKit

class MineViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
    @IBOutlet weak var tableView: UITableView!
    var webs:NSArray?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title="我的"
        webs = NSArray(contentsOfFile: NSHomeDirectory() + "/Documents/webs.plist")
        self.tableView!.delegate = self
        self.tableView!.dataSource = self
        //创建一个重用的单元格
        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell0")
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell0",
                                                                   forIndexPath: indexPath) as UITableViewCell
            let url = webs![indexPath.row] as! String
            cell.textLabel?.text = url
            return cell
    }
          override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

From the debugger:

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
bladewill
  • 9
  • 1

0 Answers0