Swift致命错误:在展开Optional值时意外发现nil

时间:2017-03-25 14:14:32

标签: json swift xcode segue uibarbuttonitem

我开发的iOS应用程序包含两个名为Main.storyboard的故事板 和Explore.storyboard,它们通过Storyboard Reference使用UIBarButtonItem合并。

Main.StoryBoard UIBarButtonItem

Explore.storyboard包含一个UIViewController,其类的功能是使用json调用REST API。以下是代码:

 import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    final let urlString = "http://tvshowapi.azurewebsites.net/tvshownewsfeed"
    @IBOutlet weak var tableView: UITableView!

    var nameArray = [String]()
    var favouriteCountArray = [String]()
    var imgURLArray = [String]()
    var contentArray = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.downloadJsonWithURL()

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

    func downloadJsonWithURL() {
        let url = NSURL(string: urlString)
        URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in
            if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [[String:Any]] {

                for tvshow in jsonObj! {
                    if let name = tvshow["screenName"] as? String {
                        self.nameArray.append(name)
                    }
                    if let cnt = tvshow["favouriteCount"] as? Int {
                        self.favouriteCountArray.append("\(cnt)")
                    }
                    if let image = tvshow["imageUrl"] as? String {
                        self.imgURLArray.append(image)
                    }
                    if let content = tvshow["content"] as? String {
                        self.contentArray.append(content)
                    }
                }


                OperationQueue.main.addOperation({
                    self.tableView.reloadData() // CRASHES HERE
                })
            }
        }).resume()
    }

    func downloadJsonWithTask() {

        let url = NSURL(string: urlString)

        var downloadTask = URLRequest(url: (url as? URL)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20)

        downloadTask.httpMethod = "GET"

        URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in

            let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)

            print(jsonData!)

        }).resume()
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell

        cell.nameLabel.textAlignment = .left
        cell.nameLabel.text = nameArray[indexPath.row]
        cell.dobLabel.text = favouriteCountArray[indexPath.row]

        let imgURL = NSURL(string: imgURLArray[indexPath.row])

        if imgURL != nil {
            let data = NSData(contentsOf: (imgURL as? URL)!)
            cell.imgView.image = UIImage(data: data as! Data)
        }

        return cell
    }

    ///for showing next detailed screen with the downloaded info
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
        vc.imageString = imgURLArray[indexPath.row]
        vc.nameString = nameArray[indexPath.row]
        vc.dobString = favouriteCountArray[indexPath.row]
        vc.contentString = contentArray[indexPath.row]

        self.navigationController?.pushViewController(vc, animated: true)
    }
}

Explore.storyboard

在运行时,错误会终止程序:

  

致命错误:在解包可选值时意外发现nil

我做错了什么?如何消除此错误?

0 个答案:

没有答案